home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / a2.0bemacs-src.lha / Emacs-19.25 / src / window.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-27  |  94.1 KB  |  3,200 lines

  1. /* Window creation, deletion and examination for GNU Emacs.
  2.    Does not include redisplay.
  3.    Copyright (C) 1985, 1986, 1987, 1993, 1994 Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU Emacs.
  6.  
  7. GNU Emacs is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU Emacs is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU Emacs; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include <config.h>
  22. #include "lisp.h"
  23. #include "buffer.h"
  24. #include "frame.h"
  25. #include "window.h"
  26. #include "commands.h"
  27. #include "indent.h"
  28. #include "termchar.h"
  29. #include "disptab.h"
  30. #include "keyboard.h"
  31.  
  32.  
  33. #ifdef AMIGA
  34. #include <assert.h> /* CHFIXME */
  35. #endif
  36.  
  37. Lisp_Object Qwindowp, Qwindow_live_p;
  38.  
  39. Lisp_Object Fnext_window (), Fdelete_window (), Fselect_window ();
  40. Lisp_Object Fset_window_buffer (), Fsplit_window (), Frecenter ();
  41.  
  42. void delete_all_subwindows ();
  43. static struct window *decode_window();
  44.  
  45. /* This is the window in which the terminal's cursor should
  46.    be left when nothing is being done with it.  This must
  47.    always be a leaf window, and its buffer is selected by
  48.    the top level editing loop at the end of each command.
  49.  
  50.    This value is always the same as
  51.    FRAME_SELECTED_WINDOW (selected_frame).  */
  52.  
  53. Lisp_Object selected_window;
  54.  
  55. /* The minibuffer window of the selected frame.
  56.    Note that you cannot test for minibufferness of an arbitrary window
  57.    by comparing against this; but you can test for minibufferness of
  58.    the selected window.  */
  59. Lisp_Object minibuf_window;
  60.  
  61. /* Non-nil means it is the window for C-M-v to scroll
  62.    when the minibuffer is selected.  */
  63. Lisp_Object Vminibuf_scroll_window;
  64.  
  65. /* Non-nil means this is the buffer whose window C-M-v should scroll.  */
  66. Lisp_Object Vother_window_scroll_buffer;
  67.  
  68. /* Non-nil means it's function to call to display temp buffers.  */
  69. Lisp_Object Vtemp_buffer_show_function;
  70.  
  71. /* If a window gets smaller than either of these, it is removed. */
  72. int window_min_height;
  73. int window_min_width;
  74.  
  75. /* Nonzero implies Fdisplay_buffer should create windows. */
  76. int pop_up_windows;
  77.  
  78. /* Nonzero implies make new frames for Fdisplay_buffer.  */
  79. int pop_up_frames;
  80.  
  81. /* Non-nil means use this function instead of default */
  82. Lisp_Object Vpop_up_frame_function;
  83.  
  84. /* Function to call to handle Fdisplay_buffer.  */
  85. Lisp_Object Vdisplay_buffer_function;
  86.  
  87. /* List of buffer *names* for buffers that should have their own frames.  */
  88. Lisp_Object Vspecial_display_buffer_names;
  89.  
  90. /* List of regexps for buffer names that should have their own frames.  */
  91. Lisp_Object Vspecial_display_regexps;
  92.  
  93. /* Function to pop up a special frame.  */
  94. Lisp_Object Vspecial_display_function;
  95.  
  96. /* Fdisplay_buffer always splits the largest window 
  97.    if that window is more than this high.  */
  98. int split_height_threshold;
  99.  
  100. /* Number of lines of continuity in scrolling by screenfuls.  */
  101. int next_screen_context_lines;
  102.  
  103. /* Incremented for each window created.  */
  104. static int sequence_number;
  105.  
  106. #define min(a, b) ((a) < (b) ? (a) : (b))
  107.  
  108. DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
  109.   "Returns t if OBJ is a window.")
  110.   (obj)
  111.      Lisp_Object obj;
  112. {
  113.   return XTYPE (obj) == Lisp_Window ? Qt : Qnil;
  114. }
  115.  
  116. DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
  117.   "Returns t if OBJ is a window which is currently visible.")
  118.      (obj)
  119.      Lisp_Object obj;
  120. {
  121.   return ((XTYPE (obj) == Lisp_Window
  122.        && ! NILP (XWINDOW (obj)->buffer))
  123.       ? Qt : Qnil);
  124. }
  125.  
  126. Lisp_Object
  127. make_window ()
  128. {
  129.   register Lisp_Object val;
  130.   register struct window *p;
  131.  
  132.   /* Add sizeof (Lisp_Object) here because sizeof (struct Lisp_Vector)
  133.      includes the first element.  */
  134.   val = Fmake_vector (
  135.     make_number ((sizeof (struct window) - sizeof (struct Lisp_Vector)
  136.           + sizeof (Lisp_Object))
  137.          / sizeof (Lisp_Object)),
  138.     Qnil);
  139.   XSETTYPE (val, Lisp_Window);
  140.   p = XWINDOW (val);
  141.   XFASTINT (p->sequence_number) = ++sequence_number;
  142.   XFASTINT (p->left) = XFASTINT (p->top)
  143.     = XFASTINT (p->height) = XFASTINT (p->width)
  144.       = XFASTINT (p->hscroll) = 0;
  145.   XFASTINT (p->last_point_x) = XFASTINT (p->last_point_y) = 0;
  146.   p->start = Fmake_marker ();
  147.   p->pointm = Fmake_marker ();
  148.   XFASTINT (p->use_time) = 0;
  149.   p->frame = Qnil;
  150.   p->display_table = Qnil;
  151.   p->dedicated = Qnil;
  152.   return val;
  153. }
  154.  
  155. DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
  156.   "Return the window that the cursor now appears in and commands apply to.")
  157.   ()
  158. {
  159.   return selected_window;
  160. }
  161.  
  162. DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
  163.   "Return the window used now for minibuffers.\n\
  164. If the optional argument FRAME is specified, return the minibuffer window\n\
  165. used by that frame.")
  166.   (frame)
  167.     Lisp_Object frame;
  168. {
  169. #ifdef MULTI_FRAME
  170.   if (NILP (frame))
  171.     XSET (frame, Lisp_Frame, selected_frame);
  172.   else
  173.     CHECK_LIVE_FRAME (frame, 0);
  174. #endif
  175.  
  176.   return FRAME_MINIBUF_WINDOW (XFRAME (frame));
  177. }
  178.  
  179. DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p, Swindow_minibuffer_p, 0, 1, 0,
  180.   "Returns non-nil if WINDOW is a minibuffer window.")
  181.   (window)
  182.      Lisp_Object window;
  183. {
  184.   struct window *w = decode_window (window);
  185.   return (MINI_WINDOW_P (w) ? Qt : Qnil);
  186. }
  187.  
  188. DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
  189.   Spos_visible_in_window_p, 0, 2, 0,
  190.   "Return t if position POS is currently on the frame in WINDOW.\n\
  191. Returns nil if that position is scrolled vertically out of view.\n\
  192. POS defaults to point; WINDOW, to the selected window.")
  193.   (pos, window)
  194.      Lisp_Object pos, window;
  195. {
  196.   register struct window *w;
  197.   register int top;
  198.   register int height;
  199.   register int posint;
  200.   register struct buffer *buf;
  201.   struct position posval;
  202.   int hscroll;
  203.  
  204.   if (NILP (pos))
  205.     posint = PT;
  206.   else
  207.     {
  208.       CHECK_NUMBER_COERCE_MARKER (pos, 0);
  209.       posint = XINT (pos);
  210.     }
  211.  
  212.   w = decode_window (window);
  213.   top = marker_position (w->start);
  214.   hscroll = XINT (w->hscroll);
  215.  
  216.   if (posint < top)
  217.     return Qnil;
  218.  
  219.   height = XFASTINT (w->height) - ! MINI_WINDOW_P (w);
  220.  
  221.   buf = XBUFFER (w->buffer);
  222.   if (XFASTINT (w->last_modified) >= BUF_MODIFF (buf))
  223.     {
  224.       /* If frame is up to date,
  225.      use the info recorded about how much text fit on it. */
  226.       if (posint < BUF_Z (buf) - XFASTINT (w->window_end_pos)
  227.       || (XFASTINT (w->window_end_vpos) < height))
  228.     return Qt;
  229.       return Qnil;
  230.     }
  231.   else
  232.     {
  233.       if (posint > BUF_ZV (buf))
  234.     return Qnil;
  235.  
  236.       /* If that info is not correct, calculate afresh */
  237.       posval = *compute_motion (top, 0, (hscroll ? 1 - hscroll : 0),
  238.                 posint, height, 0,
  239.                 window_internal_width (w) - 1,
  240.                 hscroll, 0, w);
  241.  
  242.       return posval.vpos < height ? Qt : Qnil;
  243.     }
  244. }
  245.  
  246. static struct window *
  247. decode_window (window)
  248.      register Lisp_Object window;
  249. {
  250.   if (NILP (window))
  251.     return XWINDOW (selected_window);
  252.  
  253.   CHECK_LIVE_WINDOW (window, 0);
  254.   return XWINDOW (window);
  255. }
  256.  
  257. DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
  258.   "Return the buffer that WINDOW is displaying.")
  259.   (window)
  260.      Lisp_Object window;
  261. {
  262.   return decode_window (window)->buffer;
  263. }
  264.  
  265. DEFUN ("window-height", Fwindow_height, Swindow_height, 0, 1, 0,
  266.   "Return the number of lines in WINDOW (including its mode line).")
  267.   (window)
  268.      Lisp_Object window;
  269. {
  270.   return decode_window (window)->height;
  271. }
  272.  
  273. DEFUN ("window-width", Fwindow_width, Swindow_width, 0, 1, 0,
  274.   "Return the number of display columns in WINDOW.\n\
  275. This is the width that is usable columns available for text in WINDOW.\n\
  276. If you want to find out how many columns WINDOW takes up,\n\
  277. use  (let ((edges (window-edges))) (- (nth 2 edges) (nth 0 edges))).")
  278.   (window)
  279.      Lisp_Object window;
  280. {
  281.   return make_number (window_internal_width (decode_window (window)));
  282. }
  283.  
  284. DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
  285.   "Return the number of columns by which WINDOW is scrolled from left margin.")
  286.   (window)
  287.      Lisp_Object window;
  288. {
  289.   return decode_window (window)->hscroll;
  290. }
  291.  
  292. DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
  293.   "Set number of columns WINDOW is scrolled from left margin to NCOL.\n\
  294. NCOL should be zero or positive.")
  295.   (window, ncol)
  296.      register Lisp_Object window, ncol;
  297. {
  298.   register struct window *w;
  299.  
  300.   CHECK_NUMBER (ncol, 1);
  301.   if (XINT (ncol) < 0) XFASTINT (ncol) = 0;
  302.   if (XFASTINT (ncol) >= (1 << (SHORTBITS - 1)))
  303.     args_out_of_range (ncol, Qnil);
  304.   w = decode_window (window);
  305.   if (XINT (w->hscroll) != XINT (ncol))
  306.     clip_changed = 1;        /* Prevent redisplay shortcuts */
  307.   w->hscroll = ncol;
  308.   return ncol;
  309. }
  310.  
  311. DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
  312.   "Return a list of the edge coordinates of WINDOW.\n\
  313. \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.\n\
  314. RIGHT is one more than the rightmost column used by WINDOW,\n\
  315. and BOTTOM is one more than the bottommost row used by WINDOW\n\
  316.  and its mode-line.")
  317.   (window)
  318.      Lisp_Object window;
  319. {
  320.   register struct window *w = decode_window (window);
  321.  
  322.   return Fcons (w->left, Fcons (w->top,
  323.            Fcons (make_number (XFASTINT (w->left) + XFASTINT (w->width)),
  324.           Fcons (make_number (XFASTINT (w->top)
  325.                       + XFASTINT (w->height)),
  326.              Qnil))));
  327. }
  328.  
  329. /* Test if the character at column *x, row *y is within window *w.
  330.    If it is not, return 0;
  331.    if it is in the window's text area,
  332.       set *x and *y to its location relative to the upper left corner
  333.          of the window, and
  334.       return 1;
  335.    if it is on the window's modeline, return 2;
  336.    if it is on the border between the window and its right sibling,
  337.       return 3.  */
  338. static int
  339. coordinates_in_window (w, x, y)
  340.      register struct window *w;
  341.      register int *x, *y;
  342. {
  343.   register int left = XINT (w->left);
  344.   register int width = XINT (w->width);
  345.   register int window_height = XINT (w->height);
  346.   register int top = XFASTINT (w->top);
  347.  
  348.   if (   *x < left || *x >= left + width
  349.       || *y < top  || *y >= top  + window_height)
  350.     return 0;
  351.  
  352.   /* Is the character is the mode line?  */
  353.   if (*y == top + window_height - 1
  354.       && ! MINI_WINDOW_P (w))
  355.     return 2;
  356.   
  357.   /* Is the character in the right border?  */
  358.   if (*x == left + width - 1
  359.       && left + width != FRAME_WIDTH (XFRAME (w->frame)))
  360.     return 3;
  361.  
  362.   *x -= left;
  363.   *y -= top;
  364.   return 1;
  365. }
  366.  
  367. DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
  368.   Scoordinates_in_window_p, 2, 2, 0,
  369.   "Return non-nil if COORDINATES are in WINDOW.\n\
  370. COORDINATES is a cons of the form (X . Y), X and Y being distances\n\
  371. measured in characters from the upper-left corner of the frame.\n\
  372. (0 .  0) denotes the character in the upper left corner of the\n\
  373. frame.\n\
  374. If COORDINATES are in the text portion of WINDOW,\n\
  375.    the coordinates relative to the window are returned.\n\
  376. If they are in the mode line of WINDOW, `mode-line' is returned.\n\
  377. If they are on the border between WINDOW and its right sibling,\n\
  378.    `vertical-line' is returned.")
  379.   (coordinates, window)
  380.      register Lisp_Object coordinates, window;
  381. {
  382.   int x, y;
  383.  
  384.   CHECK_LIVE_WINDOW (window, 0);
  385.   CHECK_CONS (coordinates, 1);
  386.   x = XINT (Fcar (coordinates));
  387.   y = XINT (Fcdr (coordinates));
  388.  
  389.   switch (coordinates_in_window (XWINDOW (window), &x, &y))
  390.     {
  391.     case 0:            /* NOT in window at all. */
  392.       return Qnil;
  393.  
  394.     case 1:            /* In text part of window. */
  395.       return Fcons (x, y);
  396.  
  397.     case 2:            /* In mode line of window. */
  398.       return Qmode_line;
  399.       
  400.     case 3:            /* On right border of window.  */
  401.       return Qvertical_line;
  402.  
  403.     default:
  404.       abort ();
  405.     }
  406. }
  407.  
  408. /* Find the window containing column x, row y, and return it as a
  409.    Lisp_Object.  If x, y is on the window's modeline, set *part
  410.    to 1; if it is on the separating line between the window and its
  411.    right sibling, set it to 2; otherwise set it to 0.  If there is no
  412.    window under x, y return nil and leave *part unmodified.  */
  413. Lisp_Object
  414. window_from_coordinates (frame, x, y, part)
  415.      FRAME_PTR frame;
  416.      int x, y;
  417.      int *part;
  418. {
  419.   register Lisp_Object tem, first;
  420.  
  421.   tem = first = FRAME_SELECTED_WINDOW (frame);
  422.  
  423.   do
  424.     {
  425.       int found = coordinates_in_window (XWINDOW (tem), &x, &y);
  426.  
  427.       if (found)
  428.     {
  429.       *part = found - 1;
  430.       return tem;
  431.     }
  432.  
  433.       tem = Fnext_window (tem, Qt, Qlambda);
  434.     }
  435.   while (! EQ (tem, first));
  436.   
  437.   return Qnil;
  438. }
  439.  
  440. DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
  441.   "Return window containing coordinates X and Y on FRAME.\n\
  442. If omitted, FRAME defaults to the currently selected frame.\n\
  443. The top left corner of the frame is considered to be row 0,\n\
  444. column 0.")
  445.   (x, y, frame)
  446.       Lisp_Object x, y, frame;
  447. {
  448.   int part;
  449.  
  450. #ifdef MULTI_FRAME
  451.   if (NILP (frame))
  452.     XSET (frame, Lisp_Frame, selected_frame);
  453.   else
  454.     CHECK_LIVE_FRAME (frame, 2);
  455. #endif
  456.   CHECK_NUMBER (x, 0);
  457.   CHECK_NUMBER (y, 1);
  458.  
  459.   return window_from_coordinates (XFRAME (frame),
  460.                   XINT (x), XINT (y),
  461.                   &part);
  462. }
  463.  
  464. DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
  465.   "Return current value of point in WINDOW.\n\
  466. For a nonselected window, this is the value point would have\n\
  467. if that window were selected.\n\
  468. \n\
  469. Note that, when WINDOW is the selected window and its buffer\n\
  470. is also currently selected, the value returned is the same as (point).\n\
  471. It would be more strictly correct to return the `top-level' value\n\
  472. of point, outside of any save-excursion forms.\n\
  473. But that is hard to define.")
  474.   (window)
  475.      Lisp_Object window;
  476. {
  477.   register struct window *w = decode_window (window);
  478.  
  479.   if (w == XWINDOW (selected_window)
  480.       && current_buffer == XBUFFER (w->buffer))
  481.     return Fpoint ();
  482.   return Fmarker_position (w->pointm);
  483. }
  484.  
  485. DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
  486.   "Return position at which display currently starts in WINDOW.")
  487.   (window)
  488.      Lisp_Object window;
  489. {
  490.   return Fmarker_position (decode_window (window)->start);
  491. }
  492.  
  493. DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 1, 0,
  494.   "Return position at which display currently ends in WINDOW.")
  495.   (window)
  496.      Lisp_Object window;
  497. {
  498.   Lisp_Object value;
  499.   struct window *w = decode_window (window);
  500.   Lisp_Object buf;
  501.  
  502.   buf = w->buffer;
  503.   CHECK_BUFFER (buf, 0);
  504.  
  505.   XSET (value, Lisp_Int,
  506.     BUF_Z (XBUFFER (buf)) - XFASTINT (w->window_end_pos));
  507.  
  508.   return value;
  509. }
  510.  
  511. DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
  512.   "Make point value in WINDOW be at position POS in WINDOW's buffer.")
  513.   (window, pos)
  514.      Lisp_Object window, pos;
  515. {
  516.   register struct window *w = decode_window (window);
  517.  
  518.   CHECK_NUMBER_COERCE_MARKER (pos, 1);
  519.   if (w == XWINDOW (selected_window))
  520.     Fgoto_char (pos);
  521.   else
  522.     set_marker_restricted (w->pointm, pos, w->buffer);
  523.  
  524.   return pos;
  525. }
  526.  
  527. DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
  528.   "Make display in WINDOW start at position POS in WINDOW's buffer.\n\
  529. Optional third arg NOFORCE non-nil inhibits next redisplay\n\
  530. from overriding motion of point in order to display at this exact start.")
  531.   (window, pos, noforce)
  532.      Lisp_Object window, pos, noforce;
  533. {
  534.   register struct window *w = decode_window (window);
  535.  
  536.   CHECK_NUMBER_COERCE_MARKER (pos, 1);
  537.   set_marker_restricted (w->start, pos, w->buffer);
  538.   /* this is not right, but much easier than doing what is right. */
  539.   w->start_at_line_beg = Qnil;
  540.   if (NILP (noforce))
  541.     w->force_start = Qt;
  542.   w->update_mode_line = Qt;
  543.   XFASTINT (w->last_modified) = 0;
  544.   if (!EQ (window, selected_window))
  545.     windows_or_buffers_changed++;
  546.   return pos;
  547. }
  548.  
  549. DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
  550.        1, 1, 0,
  551.   "Return WINDOW's dedicated object, usually t or nil.\n\
  552. See also `set-window-dedicated-p'.")
  553.   (window)
  554.      Lisp_Object window;
  555. {
  556.   return decode_window (window)->dedicated;
  557. }
  558.  
  559. DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
  560.        Sset_window_dedicated_p, 2, 2, 0,
  561.   "Control whether WINDOW is dedicated to the buffer it displays.\n\
  562. If it is dedicated, Emacs will not automatically change\n\
  563. which buffer appears in it.\n\
  564. The second argument is the new value for the dedication flag;\n\
  565. non-nil means yes.")
  566.   (window, arg)
  567.        Lisp_Object window, arg;
  568. {
  569.   register struct window *w = decode_window (window);
  570.  
  571.   if (NILP (arg))
  572.     w->dedicated = Qnil;
  573.   else
  574.     w->dedicated = Qt;
  575.  
  576.   return w->dedicated;
  577. }
  578.  
  579. DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
  580.        0, 1, 0,
  581.   "Return the display-table that WINDOW is using.")
  582.   (window)
  583.      Lisp_Object window;
  584. {
  585.   return decode_window (window)->display_table;
  586. }
  587.  
  588. /* Get the display table for use currently on window W.
  589.    This is either W's display table or W's buffer's display table.
  590.    Ignore the specified tables if they are not valid;
  591.    if no valid table is specified, return 0.  */
  592.  
  593. struct Lisp_Vector *
  594. window_display_table (w)
  595.      struct window *w;
  596. {
  597.   Lisp_Object tem;
  598.   assert(w!=NULL);
  599.   tem = w->display_table;
  600.   if (XTYPE (tem) == Lisp_Vector && XVECTOR (tem)->size == DISP_TABLE_SIZE)
  601.     return XVECTOR (tem);
  602.   tem = XBUFFER (w->buffer)->display_table;
  603.   if (XTYPE (tem) == Lisp_Vector && XVECTOR (tem)->size == DISP_TABLE_SIZE)
  604.     return XVECTOR (tem);
  605.   tem = Vstandard_display_table;
  606.   if (XTYPE (tem) == Lisp_Vector && XVECTOR (tem)->size == DISP_TABLE_SIZE)
  607.     return XVECTOR (tem);
  608.   return 0;
  609. }
  610.  
  611. DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
  612.   "Set WINDOW's display-table to TABLE.")
  613.   (window, table)
  614.      register Lisp_Object window, table;
  615. {
  616.   register struct window *w;
  617.   register Lisp_Object z;    /* Return value. */
  618.  
  619.   w = decode_window (window);
  620.   w->display_table = table;
  621.   return table;
  622. }
  623.  
  624. /* Record info on buffer window w is displaying
  625.    when it is about to cease to display that buffer.  */
  626. static
  627. unshow_buffer (w)
  628.      register struct window *w;
  629. {
  630.   Lisp_Object buf;
  631.  
  632.   buf = w->buffer;
  633.   if (XBUFFER (buf) != XMARKER (w->pointm)->buffer)
  634.     abort ();
  635.  
  636. #if 0
  637.   if (w == XWINDOW (selected_window)
  638.       || ! EQ (buf, XWINDOW (selected_window)->buffer))
  639.     /* Do this except when the selected window's buffer
  640.        is being removed from some other window.  */
  641. #endif
  642.     /* last_window_start records the start position that this buffer
  643.        had in the last window to be disconnected from it.
  644.        Now that this statement is unconditional,
  645.        it is possible for the buffer to be displayed in the
  646.        selected window, while last_window_start reflects another
  647.        window which was recently showing the same buffer.
  648.        Some people might say that might be a good thing.  Let's see.  */
  649.     XBUFFER (buf)->last_window_start = marker_position (w->start);
  650.  
  651.   /* Point in the selected window's buffer
  652.      is actually stored in that buffer, and the window's pointm isn't used.
  653.      So don't clobber point in that buffer.  */
  654.   if (! EQ (buf, XWINDOW (selected_window)->buffer))
  655.     BUF_PT (XBUFFER (buf))
  656.       = clip_to_bounds (BUF_BEGV (XBUFFER (buf)),
  657.             marker_position (w->pointm),
  658.             BUF_ZV (XBUFFER (buf)));
  659. }
  660.  
  661. /* Put replacement into the window structure in place of old. */
  662. static
  663. replace_window (old, replacement)
  664.      Lisp_Object old, replacement;
  665. {
  666.   register Lisp_Object tem;
  667.   register struct window *o = XWINDOW (old), *p = XWINDOW (replacement);
  668.  
  669.   /* If OLD is its frame's root_window, then replacement is the new
  670.      root_window for that frame.  */
  671.  
  672.   if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
  673.     FRAME_ROOT_WINDOW (XFRAME (o->frame)) = replacement;
  674.  
  675.   p->left = o->left;
  676.   p->top = o->top;
  677.   p->width = o->width;
  678.   p->height = o->height;
  679.  
  680.   p->next = tem = o->next;
  681.   if (!NILP (tem))
  682.     XWINDOW (tem)->prev = replacement;
  683.  
  684.   p->prev = tem = o->prev;
  685.   if (!NILP (tem))
  686.     XWINDOW (tem)->next = replacement;
  687.  
  688.   p->parent = tem = o->parent;
  689.   if (!NILP (tem))
  690.     {
  691.       if (EQ (XWINDOW (tem)->vchild, old))
  692.     XWINDOW (tem)->vchild = replacement;
  693.       if (EQ (XWINDOW (tem)->hchild, old))
  694.     XWINDOW (tem)->hchild = replacement;
  695.     }
  696.  
  697. /*** Here, if replacement is a vertical combination
  698. and so is its new parent, we should make replacement's
  699. children be children of that parent instead.  ***/
  700. }
  701.  
  702. DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
  703.   "Remove WINDOW from the display.  Default is selected window.")
  704.   (window)
  705.      register Lisp_Object window;
  706. {
  707.   register Lisp_Object tem, parent, sib;
  708.   register struct window *p;
  709.   register struct window *par;
  710.  
  711.   /* Because this function is called by other C code on non-leaf
  712.      windows, the CHECK_LIVE_WINDOW macro would choke inappropriately,
  713.      so we can't decode_window here.  */
  714.   if (NILP (window))
  715.     window = selected_window;
  716.   else
  717.     CHECK_WINDOW (window, 0);
  718.   p = XWINDOW (window);
  719.  
  720.   /* It's okay to delete an already-deleted window.  */
  721.   if (NILP (p->buffer)
  722.       && NILP (p->hchild)
  723.       && NILP (p->vchild))
  724.     return Qnil;
  725.  
  726.   parent = p->parent;
  727.   if (NILP (parent))
  728.     error ("Attempt to delete minibuffer or sole ordinary window");
  729.   par = XWINDOW (parent);
  730.  
  731.   windows_or_buffers_changed++;
  732.  
  733.   /* Are we trying to delete any frame's selected window?  */
  734.   {
  735.     Lisp_Object frame, pwindow;
  736.  
  737.     /* See if the frame's selected window is either WINDOW
  738.        or any subwindow of it, by finding all that window's parents
  739.        and comparing each one with WINDOW.  */
  740.     frame = WINDOW_FRAME (XWINDOW (window));
  741.     pwindow = FRAME_SELECTED_WINDOW (XFRAME (frame));
  742.  
  743.     while (!NILP (pwindow))
  744.       {
  745.     if (EQ (window, pwindow))
  746.       break;
  747.     pwindow = XWINDOW (pwindow)->parent;
  748.       }
  749.  
  750.     if (EQ (window, pwindow))
  751.       {
  752.     Lisp_Object alternative;
  753.     alternative = Fnext_window (window, Qlambda, Qnil);
  754.  
  755.     /* If we're about to delete the selected window on the
  756.        selected frame, then we should use Fselect_window to select
  757.        the new window.  On the other hand, if we're about to
  758.        delete the selected window on any other frame, we shouldn't do
  759.        anything but set the frame's selected_window slot.  */
  760.     if (EQ (window, selected_window))
  761.       Fselect_window (alternative);
  762.     else
  763.       FRAME_SELECTED_WINDOW (XFRAME (frame)) = alternative;
  764.       }
  765.   }
  766.  
  767.   tem = p->buffer;
  768.   /* tem is null for dummy parent windows
  769.      (which have inferiors but not any contents themselves) */
  770.   if (!NILP (tem))
  771.     {
  772.       unshow_buffer (p);
  773.       unchain_marker (p->pointm);
  774.       unchain_marker (p->start);
  775.     }
  776.  
  777.   tem = p->next;
  778.   if (!NILP (tem))
  779.     XWINDOW (tem)->prev = p->prev;
  780.  
  781.   tem = p->prev;
  782.   if (!NILP (tem))
  783.     XWINDOW (tem)->next = p->next;
  784.  
  785.   if (EQ (window, par->hchild))
  786.     par->hchild = p->next;
  787.   if (EQ (window, par->vchild))
  788.     par->vchild = p->next;
  789.  
  790.   /* Find one of our siblings to give our space to.  */
  791.   sib = p->prev;
  792.   if (NILP (sib))
  793.     {
  794.       /* If p gives its space to its next sibling, that sibling needs
  795.      to have its top/left side pulled back to where p's is.
  796.      set_window_{height,width} will re-position the sibling's
  797.      children.  */
  798.       sib = p->next;
  799.       XWINDOW (sib)->top = p->top;
  800.       XWINDOW (sib)->left = p->left;
  801.     }
  802.  
  803.   /* Stretch that sibling.  */
  804.   if (!NILP (par->vchild))
  805.     set_window_height (sib,
  806.                XFASTINT (XWINDOW (sib)->height) + XFASTINT (p->height),
  807.                1);
  808.   if (!NILP (par->hchild))
  809.     set_window_width (sib,
  810.               XFASTINT (XWINDOW (sib)->width) + XFASTINT (p->width),
  811.               1);
  812.  
  813.   /* If parent now has only one child,
  814.      put the child into the parent's place.  */
  815.   tem = par->hchild;
  816.   if (NILP (tem))
  817.     tem = par->vchild;
  818.   if (NILP (XWINDOW (tem)->next))
  819.     replace_window (parent, tem);
  820.  
  821.   /* Since we may be deleting combination windows, we must make sure that
  822.      not only p but all its children have been marked as deleted.  */
  823.   if (! NILP (p->hchild))
  824.     delete_all_subwindows (XWINDOW (p->hchild));
  825.   else if (! NILP (p->vchild))
  826.     delete_all_subwindows (XWINDOW (p->vchild));
  827.  
  828.   /* Mark this window as deleted.  */
  829.   p->buffer = p->hchild = p->vchild = Qnil;
  830.  
  831.   return Qnil;
  832. }
  833.  
  834.  
  835. extern Lisp_Object next_frame (), prev_frame ();
  836.  
  837. /* This comment supplies the doc string for `next-window',
  838.    for make-docfile to see.  We cannot put this in the real DEFUN
  839.    due to limits in the Unix cpp.
  840.  
  841. DEFUN ("next-window", Ffoo, Sfoo, 0, 3, 0,
  842.   "Return next window after WINDOW in canonical ordering of windows.\n\
  843. If omitted, WINDOW defaults to the selected window.\n\
  844. \n\
  845. Optional second arg MINIBUF t means count the minibuffer window even\n\
  846. if not active.  MINIBUF nil or omitted means count the minibuffer iff\n\
  847. it is active.  MINIBUF neither t nor nil means not to count the\n\
  848. minibuffer even if it is active.\n\
  849. \n\
  850. Several frames may share a single minibuffer; if the minibuffer\n\
  851. counts, all windows on all frames that share that minibuffer count\n\
  852. too.  This means that next-window may be used to iterate through the\n\
  853. set of windows even when the minibuffer is on another frame.  If the\n\
  854. minibuffer does not count, only windows from WINDOW's frame count.\n\
  855. \n\
  856. Optional third arg ALL-FRAMES t means include windows on all frames.\n\
  857. ALL-FRAMES nil or omitted means cycle within the frames as specified\n\
  858. above.  ALL-FRAMES = `visible' means include windows on all visible frames.\n\
  859. Anything else means restrict to WINDOW's frame.\n\
  860. \n\
  861. If you use consistent values for MINIBUF and ALL-FRAMES, you can use\n\
  862. `next-window' to iterate through the entire cycle of acceptable\n\
  863. windows, eventually ending up back at the window you started with.\n\
  864. `previous-window' traverses the same cycle, in the reverse order.")
  865.   (window, minibuf, all_frames) */
  866.  
  867. DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
  868.        0)
  869.   (window, minibuf, all_frames)
  870.      register Lisp_Object window, minibuf, all_frames;
  871. {
  872.   register Lisp_Object tem;
  873.   Lisp_Object start_window;
  874.  
  875.   if (NILP (window))
  876.     window = selected_window;
  877.   else
  878.     CHECK_LIVE_WINDOW (window, 0);
  879.  
  880.   start_window = window;
  881.  
  882.   /* minibuf == nil may or may not include minibuffers.
  883.      Decide if it does.  */
  884.   if (NILP (minibuf))
  885.     minibuf = (minibuf_level ? Qt : Qlambda);
  886.  
  887. #ifdef MULTI_FRAME
  888.   /* all_frames == nil doesn't specify which frames to include.
  889.      Decide which frames it includes.  */
  890.   if (NILP (all_frames))
  891.     all_frames = (EQ (minibuf, Qt)
  892.            ? (FRAME_MINIBUF_WINDOW
  893.               (XFRAME
  894.                (WINDOW_FRAME
  895.             (XWINDOW (window)))))
  896.            : Qnil);
  897.   else if (EQ (all_frames, Qvisible))
  898.     ;
  899.   else if (! EQ (all_frames, Qt))
  900.     all_frames = Qnil;
  901.   /* Now all_frames is t meaning search all frames,
  902.      nil meaning search just current frame,
  903.      or a window, meaning search the frame that window belongs to.  */
  904. #endif
  905.  
  906.   /* Do this loop at least once, to get the next window, and perhaps
  907.      again, if we hit the minibuffer and that is not acceptable.  */
  908.   do
  909.     {
  910.       /* Find a window that actually has a next one.  This loop
  911.      climbs up the tree.  */
  912.       while (tem = XWINDOW (window)->next, NILP (tem))
  913.     if (tem = XWINDOW (window)->parent, !NILP (tem))
  914.       window = tem;
  915.     else
  916.       {
  917.         /* We've reached the end of this frame.
  918.            Which other frames are acceptable?  */
  919.         tem = WINDOW_FRAME (XWINDOW (window));
  920. #ifdef MULTI_FRAME
  921.         if (! NILP (all_frames))
  922.           tem = next_frame (tem, all_frames);
  923. #endif
  924.         tem = FRAME_ROOT_WINDOW (XFRAME (tem));
  925.  
  926.         break;
  927.       }
  928.  
  929.       window = tem;
  930.  
  931.       /* If we're in a combination window, find its first child and
  932.      recurse on that.  Otherwise, we've found the window we want.  */
  933.       while (1)
  934.     {
  935.       if (!NILP (XWINDOW (window)->hchild))
  936.         window = XWINDOW (window)->hchild;
  937.       else if (!NILP (XWINDOW (window)->vchild))
  938.         window = XWINDOW (window)->vchild;
  939.       else break;
  940.     }
  941.     }
  942.   /* Which windows are acceptible?
  943.      Exit the loop and accept this window if
  944.      this isn't a minibuffer window, or
  945.      we're accepting minibuffer windows, or
  946.      we've come all the way around and we're back at the original window.  */
  947.   while (MINI_WINDOW_P (XWINDOW (window))
  948.      && ! EQ (minibuf, Qt)
  949.      && ! EQ (window, start_window));
  950.  
  951.   return window;
  952. }
  953.  
  954. /* This comment supplies the doc string for `previous-window',
  955.    for make-docfile to see.  We cannot put this in the real DEFUN
  956.    due to limits in the Unix cpp.
  957.  
  958. DEFUN ("previous-window", Ffoo, Sfoo, 0, 3, 0,
  959.   "Return the window preceeding WINDOW in canonical ordering of windows.\n\
  960. If omitted, WINDOW defaults to the selected window.\n\
  961. \n\
  962. Optional second arg MINIBUF t means count the minibuffer window even\n\
  963. if not active.  MINIBUF nil or omitted means count the minibuffer iff\n\
  964. it is active.  MINIBUF neither t nor nil means not to count the\n\
  965. minibuffer even if it is active.\n\
  966. \n\
  967. Several frames may share a single minibuffer; if the minibuffer\n\
  968. counts, all windows on all frames that share that minibuffer count\n\
  969. too.  This means that previous-window may be used to iterate through\n\
  970. the set of windows even when the minibuffer is on another frame.  If\n\
  971. the minibuffer does not count, only windows from WINDOW's frame\n\
  972. count.\n\
  973. \n\
  974. Optional third arg ALL-FRAMES t means include windows on all frames.\n\
  975. ALL-FRAMES nil or omitted means cycle within the frames as specified\n\
  976. above.  ALL-FRAMES = `visible' means include windows on all visible frames.\n\
  977. Anything else means restrict to WINDOW's frame.\n\
  978. \n\
  979. If you use consistent values for MINIBUF and ALL-FRAMES, you can use\n\
  980. `previous-window' to iterate through the entire cycle of acceptable\n\
  981. windows, eventually ending up back at the window you started with.\n\
  982. `next-window' traverses the same cycle, in the reverse order.")
  983.   (window, minibuf, all_frames)  */
  984.  
  985.  
  986. DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
  987.        0)
  988.   (window, minibuf, all_frames)
  989.      register Lisp_Object window, minibuf, all_frames;
  990. {
  991.   register Lisp_Object tem;
  992.   Lisp_Object start_window;
  993.  
  994.   if (NILP (window))
  995.     window = selected_window;
  996.   else
  997.     CHECK_LIVE_WINDOW (window, 0);
  998.  
  999.   start_window = window;
  1000.  
  1001.   /* minibuf == nil may or may not include minibuffers.
  1002.      Decide if it does.  */
  1003.   if (NILP (minibuf))
  1004.     minibuf = (minibuf_level ? Qt : Qlambda);
  1005.  
  1006. #ifdef MULTI_FRAME
  1007.   /* all_frames == nil doesn't specify which frames to include.
  1008.      Decide which frames it includes.  */
  1009.   if (NILP (all_frames))
  1010.     all_frames = (EQ (minibuf, Qt)
  1011.            ? (FRAME_MINIBUF_WINDOW
  1012.               (XFRAME
  1013.                (WINDOW_FRAME
  1014.             (XWINDOW (window)))))
  1015.            : Qnil);
  1016.   else if (EQ (all_frames, Qvisible))
  1017.     ;
  1018.   else if (! EQ (all_frames, Qt))
  1019.     all_frames = Qnil;
  1020.   /* Now all_frames is t meaning search all frames,
  1021.      nil meaning search just current frame,
  1022.      or a window, meaning search the frame that window belongs to.  */
  1023. #endif
  1024.  
  1025.   /* Do this loop at least once, to get the previous window, and perhaps
  1026.      again, if we hit the minibuffer and that is not acceptable.  */
  1027.   do
  1028.     {
  1029.       /* Find a window that actually has a previous one.  This loop
  1030.      climbs up the tree.  */
  1031.       while (tem = XWINDOW (window)->prev, NILP (tem))
  1032.     if (tem = XWINDOW (window)->parent, !NILP (tem))
  1033.       window = tem;
  1034.     else
  1035.       {
  1036.         /* We have found the top window on the frame.
  1037.            Which frames are acceptable?  */
  1038.         tem = WINDOW_FRAME (XWINDOW (window));
  1039. #ifdef MULTI_FRAME
  1040.         if (! NILP (all_frames))
  1041.           /* It's actually important that we use prev_frame here,
  1042.          rather than next_frame.  All the windows acceptable
  1043.          according to the given parameters should form a ring;
  1044.          Fnext_window and Fprevious_window should go back and
  1045.          forth around the ring.  If we use next_frame here,
  1046.          then Fnext_window and Fprevious_window take different
  1047.          paths through the set of acceptable windows.
  1048.          window_loop assumes that these `ring' requirement are
  1049.          met.  */
  1050.           tem = prev_frame (tem, all_frames);
  1051. #endif
  1052.         /* If this frame has a minibuffer, find that window first,
  1053.            because it is conceptually the last window in that frame.  */
  1054.         if (FRAME_HAS_MINIBUF_P (XFRAME (tem)))
  1055.           tem = FRAME_MINIBUF_WINDOW (XFRAME (tem));
  1056.         else
  1057.           tem = FRAME_ROOT_WINDOW (XFRAME (tem));
  1058.  
  1059.         break;
  1060.       }
  1061.  
  1062.       window = tem;
  1063.       /* If we're in a combination window, find its last child and
  1064.      recurse on that.  Otherwise, we've found the window we want.  */
  1065.       while (1)
  1066.     {
  1067.       if (!NILP (XWINDOW (window)->hchild))
  1068.         window = XWINDOW (window)->hchild;
  1069.       else if (!NILP (XWINDOW (window)->vchild))
  1070.         window = XWINDOW (window)->vchild;
  1071.       else break;
  1072.       while (tem = XWINDOW (window)->next, !NILP (tem))
  1073.         window = tem;
  1074.     }
  1075.     }
  1076.   /* Which windows are acceptable?
  1077.      Exit the loop and accept this window if
  1078.      this isn't a minibuffer window, or
  1079.      we're accepting minibuffer windows, or
  1080.      we've come all the way around and we're back at the original window.  */
  1081.   while (MINI_WINDOW_P (XWINDOW (window))
  1082.      && !EQ (minibuf, Qt)
  1083.      && !EQ (window, start_window));
  1084.  
  1085.   return window;
  1086. }
  1087.  
  1088. DEFUN ("other-window", Fother_window, Sother_window, 1, 2, "p",
  1089.   "Select the ARG'th different window on this frame.\n\
  1090. All windows on current frame are arranged in a cyclic order.\n\
  1091. This command selects the window ARG steps away in that order.\n\
  1092. A negative ARG moves in the opposite order.  If the optional second\n\
  1093. argument ALL_FRAMES is non-nil, cycle through all frames.")
  1094.   (n, all_frames)
  1095.      register Lisp_Object n, all_frames;
  1096. {
  1097.   register int i;
  1098.   register Lisp_Object w;
  1099.  
  1100.   CHECK_NUMBER (n, 0);
  1101.   w = selected_window;
  1102.   i = XINT (n);
  1103.  
  1104.   while (i > 0)
  1105.     {
  1106.       w = Fnext_window (w, Qnil, all_frames);
  1107.       i--;
  1108.     }
  1109.   while (i < 0)
  1110.     {
  1111.       w = Fprevious_window (w, Qnil, all_frames);
  1112.       i++;
  1113.     }
  1114.   Fselect_window (w);
  1115.   return Qnil;
  1116. }
  1117.  
  1118. /* Look at all windows, performing an operation specified by TYPE
  1119.    with argument OBJ.
  1120.    If FRAMES is Qt, look at all frames;
  1121.                 Qnil, look at just the selected frame;
  1122.         Qvisible, look at visible frames;
  1123.             a frame, just look at windows on that frame.
  1124.    If MINI is non-zero, perform the operation on minibuffer windows too.
  1125. */
  1126.  
  1127. enum window_loop
  1128. {
  1129.   WINDOW_LOOP_UNUSED,
  1130.   GET_BUFFER_WINDOW,        /* Arg is buffer */
  1131.   GET_LRU_WINDOW,        /* Arg is t for full-width windows only */
  1132.   DELETE_OTHER_WINDOWS,        /* Arg is window not to delete */
  1133.   DELETE_BUFFER_WINDOWS,    /* Arg is buffer */
  1134.   GET_LARGEST_WINDOW,
  1135.   UNSHOW_BUFFER        /* Arg is buffer */
  1136. };
  1137.  
  1138. static Lisp_Object
  1139. window_loop (type, obj, mini, frames)
  1140.      enum window_loop type;
  1141.      register Lisp_Object obj, frames;
  1142.      int mini;
  1143. {
  1144.   register Lisp_Object w;
  1145.   register Lisp_Object best_window;
  1146.   register Lisp_Object next_window;
  1147.   register Lisp_Object last_window;
  1148.   FRAME_PTR frame;
  1149.   Lisp_Object frame_arg;
  1150.   frame_arg = Qt;
  1151.  
  1152. #ifdef MULTI_FRAME
  1153.   /* If we're only looping through windows on a particular frame,
  1154.      frame points to that frame.  If we're looping through windows
  1155.      on all frames, frame is 0.  */
  1156.   if (FRAMEP (frames))
  1157.     frame = XFRAME (frames);
  1158.   else if (NILP (frames))
  1159.     frame = selected_frame;
  1160.   else
  1161.     frame = 0;
  1162.   if (frame)
  1163.     frame_arg = Qlambda;
  1164.   else if (EQ (frames, Qvisible))
  1165.     frame_arg = frames;
  1166. #else
  1167.   frame = 0;
  1168. #endif
  1169.  
  1170.   /* frame_arg is Qlambda to stick to one frame,
  1171.      Qvisible to consider all visible frames,
  1172.      or Qt otherwise.  */
  1173.  
  1174.   /* Pick a window to start with.  */
  1175.   if (XTYPE (obj) == Lisp_Window)
  1176.     w = obj;
  1177.   else if (frame)
  1178.     w = FRAME_SELECTED_WINDOW (frame);
  1179.   else
  1180.     w = FRAME_SELECTED_WINDOW (selected_frame);
  1181.  
  1182.   /* Figure out the last window we're going to mess with.  Since
  1183.      Fnext_window, given the same options, is guaranteed to go in a
  1184.      ring, we can just use Fprevious_window to find the last one.
  1185.  
  1186.      We can't just wait until we hit the first window again, because
  1187.      it might be deleted.  */
  1188.  
  1189.   last_window = Fprevious_window (w, mini ? Qt : Qnil, frame_arg);
  1190.  
  1191.   best_window = Qnil;
  1192.   for (;;)
  1193.     {
  1194.       FRAME_PTR w_frame = XFRAME (WINDOW_FRAME (XWINDOW (w)));
  1195.  
  1196.       /* Pick the next window now, since some operations will delete
  1197.      the current window.  */
  1198.       next_window = Fnext_window (w, mini ? Qt : Qnil, frame_arg);
  1199.  
  1200.       /* Note that we do not pay attention here to whether
  1201.      the frame is visible, since Fnext_window skips non-visible frames
  1202.      if that is desired, under the control of frame_arg.  */
  1203.       if (! MINI_WINDOW_P (XWINDOW (w))
  1204.       || (mini && minibuf_level > 0))
  1205.     switch (type)
  1206.       {
  1207.       case GET_BUFFER_WINDOW:
  1208.         if (XBUFFER (XWINDOW (w)->buffer) == XBUFFER (obj))
  1209.           return w;
  1210.         break;
  1211.  
  1212.       case GET_LRU_WINDOW:
  1213.         /* t as arg means consider only full-width windows */
  1214.         if (!NILP (obj) && XFASTINT (XWINDOW (w)->width)
  1215.         != FRAME_WIDTH (XFRAME (WINDOW_FRAME (XWINDOW (w)))))
  1216.           break;
  1217.         /* Ignore dedicated windows and minibuffers.  */
  1218.         if (MINI_WINDOW_P (XWINDOW (w))
  1219.         || !NILP (XWINDOW (w)->dedicated))
  1220.           break;
  1221.         if (NILP (best_window)
  1222.         || (XFASTINT (XWINDOW (best_window)->use_time)
  1223.             > XFASTINT (XWINDOW (w)->use_time)))
  1224.           best_window = w;
  1225.         break;
  1226.  
  1227.       case DELETE_OTHER_WINDOWS:
  1228.         if (XWINDOW (w) != XWINDOW (obj))
  1229.           Fdelete_window (w);
  1230.         break;
  1231.  
  1232.       case DELETE_BUFFER_WINDOWS:
  1233.         if (EQ (XWINDOW (w)->buffer, obj))
  1234.           {
  1235.         /* If we're deleting the buffer displayed in the only window
  1236.            on the frame, find a new buffer to display there.  */
  1237.         if (NILP (XWINDOW (w)->parent))
  1238.           {
  1239.             Lisp_Object new_buffer;
  1240.             new_buffer = Fother_buffer (obj, Qnil);
  1241.             if (NILP (new_buffer))
  1242.               new_buffer
  1243.             = Fget_buffer_create (build_string ("*scratch*"));
  1244.             Fset_window_buffer (w, new_buffer);
  1245.             if (EQ (w, selected_window))
  1246.               Fset_buffer (XWINDOW (w)->buffer);
  1247.           }
  1248.         else
  1249.           Fdelete_window (w);
  1250.           }
  1251.         break;
  1252.  
  1253.       case GET_LARGEST_WINDOW:
  1254.         /* Ignore dedicated windows and minibuffers.  */
  1255.         if (MINI_WINDOW_P (XWINDOW (w))
  1256.         || !NILP (XWINDOW (w)->dedicated))
  1257.           break;
  1258.         {
  1259.           struct window *best_window_ptr = XWINDOW (best_window);
  1260.           struct window *w_ptr = XWINDOW (w);
  1261.           if (NILP (best_window)
  1262.           || (XFASTINT (w_ptr->height) * XFASTINT (w_ptr->width)
  1263.               > (XFASTINT (best_window_ptr->height)
  1264.              * XFASTINT (best_window_ptr->width))))
  1265.         best_window = w;
  1266.         }
  1267.         break;
  1268.  
  1269.       case UNSHOW_BUFFER:
  1270.         if (EQ (XWINDOW (w)->buffer, obj))
  1271.           {
  1272.         /* Find another buffer to show in this window.  */
  1273.         Lisp_Object another_buffer;
  1274.         FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (w)));
  1275.         another_buffer = Fother_buffer (obj, Qnil);
  1276.         if (NILP (another_buffer))
  1277.           another_buffer
  1278.             = Fget_buffer_create (build_string ("*scratch*"));
  1279. #ifdef MULTI_FRAME
  1280.         /* If this window is dedicated, and in a frame of its own,
  1281.            kill the frame.  */
  1282.         if (EQ (w, FRAME_ROOT_WINDOW (f))
  1283.             && !NILP (XWINDOW (w)->dedicated)
  1284.             && other_visible_frames (f))
  1285.           Fdelete_frame (WINDOW_FRAME (XWINDOW (w)), Qnil);
  1286.         else
  1287. #endif
  1288.           {
  1289.             /* Otherwise show a different buffer in the window.  */
  1290.             XWINDOW (w)->dedicated = Qnil;
  1291.             Fset_window_buffer (w, another_buffer);
  1292.             if (EQ (w, selected_window))
  1293.               Fset_buffer (XWINDOW (w)->buffer);
  1294.           }
  1295.           }
  1296.         break;
  1297.       }
  1298.  
  1299.       if (EQ (w, last_window))
  1300.     break;
  1301.  
  1302.       w = next_window;
  1303.     }
  1304.  
  1305.   return best_window;
  1306. }     
  1307.  
  1308. DEFUN ("get-lru-window", Fget_lru_window, Sget_lru_window, 0, 1, 0,
  1309.   "Return the window least recently selected or used for display.\n\
  1310. If optional argument FRAME is `visible', search all visible frames.\n\
  1311. If FRAME is t, search all frames.\n\
  1312. If FRAME is nil, search only the selected frame.\n\
  1313. If FRAME is a frame, search only that frame.")
  1314.   (frame)
  1315.     Lisp_Object frame;
  1316. {
  1317.   register Lisp_Object w;
  1318.   /* First try for a window that is full-width */
  1319.   w = window_loop (GET_LRU_WINDOW, Qt, 0, frame);
  1320.   if (!NILP (w) && !EQ (w, selected_window))
  1321.     return w;
  1322.   /* If none of them, try the rest */
  1323.   return window_loop (GET_LRU_WINDOW, Qnil, 0, frame);
  1324. }
  1325.  
  1326. DEFUN ("get-largest-window", Fget_largest_window, Sget_largest_window, 0, 1, 0,
  1327.   "Return the largest window in area.\n\
  1328. If optional argument FRAME is `visible', search all visible frames.\n\
  1329. If FRAME is t, search all frames.\n\
  1330. If FRAME is nil, search only the selected frame.\n\
  1331. If FRAME is a frame, search only that frame.")
  1332.   (frame)
  1333.     Lisp_Object frame;
  1334. {
  1335.   return window_loop (GET_LARGEST_WINDOW, Qnil, 0,
  1336.               frame);
  1337. }
  1338.  
  1339. DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 1, 2, 0,
  1340.   "Return a window currently displaying BUFFER, or nil if none.\n\
  1341. If optional argument FRAME is `visible', search all visible frames.\n\
  1342. If FRAME is t, search all frames.\n\
  1343. If FRAME is nil, search only the selected frame.\n\
  1344. If FRAME is a frame, search only that frame.")
  1345.   (buffer, frame)
  1346.     Lisp_Object buffer, frame;
  1347. {
  1348.   buffer = Fget_buffer (buffer);
  1349.   if (XTYPE (buffer) == Lisp_Buffer)
  1350.     return window_loop (GET_BUFFER_WINDOW, buffer, 1, frame);
  1351.   else
  1352.     return Qnil;
  1353. }
  1354.  
  1355. DEFUN ("delete-other-windows", Fdelete_other_windows, Sdelete_other_windows,
  1356.   0, 1, "",
  1357.   "Make WINDOW (or the selected window) fill its frame.\n\
  1358. Only the frame WINDOW is on is affected.\n\
  1359. This function tries to reduce display jumps\n\
  1360. by keeping the text previously visible in WINDOW\n\
  1361. in the same place on the frame.  Doing this depends on\n\
  1362. the value of (window-start WINDOW), so if calling this function\n\
  1363. in a program gives strange scrolling, make sure the window-start\n\
  1364. value is reasonable when this function is called.")
  1365.   (window)
  1366.      Lisp_Object window;
  1367. {
  1368.   struct window *w;
  1369.   int startpos;
  1370.   int top;
  1371.  
  1372.   if (NILP (window))
  1373.     window = selected_window;
  1374.   else
  1375.     CHECK_LIVE_WINDOW (window, 0);
  1376.  
  1377.   w = XWINDOW (window);
  1378.   startpos = marker_position (w->start);
  1379.   top = XFASTINT (w->top) - FRAME_MENU_BAR_LINES (XFRAME (WINDOW_FRAME (w)));
  1380.  
  1381.   window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME (w));
  1382.  
  1383.   /* Try to minimize scrolling, by setting the window start to the point
  1384.      will cause the text at the old window start to be at the same place
  1385.      on the frame.  But don't try to do this if the window start is
  1386.      outside the visible portion (as might happen when the display is
  1387.      not current, due to typeahead).  */
  1388.   if (startpos >= BUF_BEGV (XBUFFER (w->buffer))
  1389.       && startpos <= BUF_ZV (XBUFFER (w->buffer)))
  1390.     {
  1391.       struct position pos;
  1392.       struct buffer *obuf = current_buffer;
  1393.  
  1394.       Fset_buffer (w->buffer);
  1395.       /* This computation used to temporarily move point, but that can
  1396.      have unwanted side effects due to text properties.  */
  1397.       pos = *vmotion (startpos, -top, window_internal_width (w) - 1,
  1398.               XINT (w->hscroll), window);
  1399.       Fset_marker (w->start, make_number (pos.bufpos), w->buffer);
  1400.       w->start_at_line_beg = ((pos.bufpos == BEGV
  1401.                    || FETCH_CHAR (pos.bufpos - 1) == '\n') ? Qt
  1402.                   : Qnil);
  1403.  
  1404.       set_buffer_internal (obuf);
  1405.     }
  1406.   return Qnil;
  1407. }
  1408.  
  1409. DEFUN ("delete-windows-on", Fdelete_windows_on, Sdelete_windows_on,
  1410.   1, 2, "bDelete windows on (buffer): ",
  1411.   "Delete all windows showing BUFFER.\n\
  1412. Optional second argument FRAME controls which frames are affected.\n\
  1413. If nil or omitted, delete all windows showing BUFFER in any frame.\n\
  1414. If t, delete only windows showing BUFFER in the selected frame.\n\
  1415. If `visible', delete all windows showing BUFFER in any visible frame.\n\
  1416. If a frame, delete only windows showing BUFFER in that frame.")
  1417.   (buffer, frame)
  1418.      Lisp_Object buffer, frame;
  1419. {
  1420. #ifdef MULTI_FRAME
  1421.   /* FRAME uses t and nil to mean the opposite of what window_loop
  1422.      expects.  */
  1423.   if (! FRAMEP (frame))
  1424.     frame = NILP (frame) ? Qt : Qnil;
  1425. #else
  1426.   frame = Qt;
  1427. #endif
  1428.  
  1429.   if (!NILP (buffer))
  1430.     {
  1431.       buffer = Fget_buffer (buffer);
  1432.       CHECK_BUFFER (buffer, 0);
  1433.       window_loop (DELETE_BUFFER_WINDOWS, buffer, 0, frame);
  1434.     }
  1435.   return Qnil;
  1436. }
  1437.  
  1438. DEFUN ("replace-buffer-in-windows", Freplace_buffer_in_windows,
  1439.   Sreplace_buffer_in_windows,
  1440.   1, 1, "bReplace buffer in windows: ",
  1441.   "Replace BUFFER with some other buffer in all windows showing it.")
  1442.   (buffer)
  1443.      Lisp_Object buffer;
  1444. {
  1445.   if (!NILP (buffer))
  1446.     {
  1447.       buffer = Fget_buffer (buffer);
  1448.       CHECK_BUFFER (buffer, 0);
  1449.       window_loop (UNSHOW_BUFFER, buffer, 0, Qt);
  1450.     }
  1451.   return Qnil;
  1452. }
  1453.  
  1454. /* Set the height of WINDOW and all its inferiors.  */
  1455.  
  1456. /* The smallest acceptable dimensions for a window.  Anything smaller
  1457.    might crash Emacs.  */
  1458. #define MIN_SAFE_WINDOW_WIDTH  (2)
  1459. #define MIN_SAFE_WINDOW_HEIGHT (2)
  1460.  
  1461. /* Make sure that window_min_height and window_min_width are
  1462.    not too small; if they are, set them to safe minima.  */
  1463.  
  1464. static void
  1465. check_min_window_sizes ()
  1466. {
  1467.   /* Smaller values might permit a crash.  */
  1468.   if (window_min_width < MIN_SAFE_WINDOW_WIDTH)
  1469.     window_min_width = MIN_SAFE_WINDOW_WIDTH;
  1470.   if (window_min_height < MIN_SAFE_WINDOW_HEIGHT)
  1471.     window_min_height = MIN_SAFE_WINDOW_HEIGHT;
  1472. }
  1473.  
  1474. /* If *ROWS or *COLS are too small a size for FRAME, set them to the
  1475.    minimum allowable size.  */
  1476. void
  1477. check_frame_size (frame, rows, cols)
  1478.      FRAME_PTR frame;
  1479.      int *rows, *cols;
  1480. {
  1481.   /* For height, we have to see:
  1482.      whether the frame has a minibuffer, 
  1483.      whether it wants a mode line, and
  1484.      whether it has a menu bar.  */
  1485.   int min_height =
  1486.     (FRAME_MINIBUF_ONLY_P (frame) ? MIN_SAFE_WINDOW_HEIGHT - 1
  1487.      : (! FRAME_HAS_MINIBUF_P (frame)) ? MIN_SAFE_WINDOW_HEIGHT
  1488.      : 2 * MIN_SAFE_WINDOW_HEIGHT - 1);
  1489.   if (FRAME_MENU_BAR_LINES (frame) > 0)
  1490.     min_height += FRAME_MENU_BAR_LINES (frame);
  1491.  
  1492.   if (*rows < min_height)
  1493.     *rows = min_height;
  1494.   if (*cols  < MIN_SAFE_WINDOW_WIDTH)
  1495.     *cols = MIN_SAFE_WINDOW_WIDTH;
  1496. }
  1497.  
  1498. /* Normally the window is deleted if it gets too small.
  1499.    nodelete nonzero means do not do this.
  1500.    (The caller should check later and do so if appropriate)  */
  1501.  
  1502. set_window_height (window, height, nodelete)
  1503.      Lisp_Object window;
  1504.      int height;
  1505.      int nodelete;
  1506. {
  1507.   register struct window *w = XWINDOW (window);
  1508.   register struct window *c;
  1509.   int oheight = XFASTINT (w->height);
  1510.   int top, pos, lastbot, opos, lastobot;
  1511.   Lisp_Object child;
  1512.  
  1513.   check_min_window_sizes ();
  1514.  
  1515.   if (!nodelete
  1516.       && ! NILP (w->parent)
  1517.       && height < window_min_height)
  1518.     {
  1519.       Fdelete_window (window);
  1520.       return;
  1521.     }
  1522.  
  1523.   XFASTINT (w->last_modified) = 0;
  1524.   windows_or_buffers_changed++;
  1525.   XFASTINT (w->height) = height;
  1526.   if (!NILP (w->hchild))
  1527.     {
  1528.       for (child = w->hchild; !NILP (child); child = XWINDOW (child)->next)
  1529.     {
  1530.       XWINDOW (child)->top = w->top;
  1531.       set_window_height (child, height, nodelete);
  1532.     }
  1533.     }
  1534.   else if (!NILP (w->vchild))
  1535.     {
  1536.       lastbot = top = XFASTINT (w->top);
  1537.       lastobot = 0;
  1538.       for (child = w->vchild; !NILP (child); child = c->next)
  1539.     {
  1540.       c = XWINDOW (child);
  1541.  
  1542.       opos = lastobot + XFASTINT (c->height);
  1543.  
  1544.       XFASTINT (c->top) = lastbot;
  1545.  
  1546.       pos = (((opos * height) << 1) + oheight) / (oheight << 1);
  1547.  
  1548.       /* Avoid confusion: inhibit deletion of child if becomes too small */
  1549.       set_window_height (child, pos + top - lastbot, 1);
  1550.  
  1551.       /* Now advance child to next window,
  1552.          and set lastbot if child was not just deleted.  */
  1553.       lastbot = pos + top;
  1554.       lastobot = opos;
  1555.     }
  1556.       /* Now delete any children that became too small.  */
  1557.       if (!nodelete)
  1558.     for (child = w->vchild; !NILP (child); child = XWINDOW (child)->next)
  1559.       {
  1560.         set_window_height (child, XINT (XWINDOW (child)->height), 0);
  1561.       }
  1562.     }
  1563. }
  1564.  
  1565. /* Recursively set width of WINDOW and its inferiors. */
  1566.  
  1567. set_window_width (window, width, nodelete)
  1568.      Lisp_Object window;
  1569.      int width;
  1570.      int nodelete;
  1571. {
  1572.   register struct window *w = XWINDOW (window);
  1573.   register struct window *c;
  1574.   int owidth = XFASTINT (w->width);
  1575.   int left, pos, lastright, opos, lastoright;
  1576.   Lisp_Object child;
  1577.  
  1578.   if (!nodelete && width < window_min_width && !NILP (w->parent))
  1579.     {
  1580.       Fdelete_window (window);
  1581.       return;
  1582.     }
  1583.  
  1584.   XFASTINT (w->last_modified) = 0;
  1585.   windows_or_buffers_changed++;
  1586.   XFASTINT (w->width) = width;
  1587.   if (!NILP (w->vchild))
  1588.     {
  1589.       for (child = w->vchild; !NILP (child); child = XWINDOW (child)->next)
  1590.     {
  1591.       XWINDOW (child)->left = w->left;
  1592.       set_window_width (child, width, nodelete);
  1593.     }
  1594.     }
  1595.   else if (!NILP (w->hchild))
  1596.     {
  1597.       lastright = left = XFASTINT (w->left);
  1598.       lastoright = 0;
  1599.       for (child = w->hchild; !NILP (child); child = c->next)
  1600.     {
  1601.       c = XWINDOW (child);
  1602.  
  1603.       opos = lastoright + XFASTINT (c->width);
  1604.  
  1605.       XFASTINT (c->left) = lastright;
  1606.  
  1607.       pos = (((opos * width) << 1) + owidth) / (owidth << 1);
  1608.  
  1609.       /* Inhibit deletion for becoming too small */
  1610.       set_window_width (child, pos + left - lastright, 1);
  1611.  
  1612.       /* Now advance child to next window,
  1613.          and set lastright if child was not just deleted.  */
  1614.       lastright = pos + left, lastoright = opos;
  1615.     }
  1616.       /* Delete children that became too small */
  1617.       if (!nodelete)
  1618.     for (child = w->hchild; !NILP (child); child = XWINDOW (child)->next)
  1619.       {
  1620.         set_window_width (child, XINT (XWINDOW (child)->width), 0);
  1621.       }
  1622.     }
  1623. }
  1624.  
  1625. int window_select_count;
  1626.  
  1627. DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 2, 0,
  1628.   "Make WINDOW display BUFFER as its contents.\n\
  1629. BUFFER can be a buffer or buffer name.")
  1630.   (window, buffer)
  1631.      register Lisp_Object window, buffer;
  1632. {
  1633.   register Lisp_Object tem;
  1634.   register struct window *w = decode_window (window);
  1635.  
  1636.   buffer = Fget_buffer (buffer);
  1637.   CHECK_BUFFER (buffer, 1);
  1638.  
  1639.   if (NILP (XBUFFER (buffer)->name))
  1640.     error ("Attempt to display deleted buffer");
  1641.  
  1642.   tem = w->buffer;
  1643.   if (NILP (tem))
  1644.     error ("Window is deleted");
  1645.   else if (! EQ (tem, Qt))    /* w->buffer is t when the window
  1646.                    is first being set up.  */
  1647.     {
  1648.       if (!NILP (w->dedicated) && !EQ (tem, buffer))
  1649.     error ("Window is dedicated to `%s'",
  1650.            XSTRING (XBUFFER (tem)->name)->data);
  1651.  
  1652.       unshow_buffer (w);
  1653.     }
  1654.  
  1655.   w->buffer = buffer;
  1656.   w->window_end_pos = 0;
  1657.   w->window_end_valid = Qnil;
  1658.   w->hscroll = 0;
  1659.   Fset_marker (w->pointm,
  1660.            make_number (BUF_PT (XBUFFER (buffer))),
  1661.            buffer);
  1662.   set_marker_restricted (w->start,
  1663.              make_number (XBUFFER (buffer)->last_window_start),
  1664.              buffer);
  1665.   w->start_at_line_beg = Qnil;
  1666.   w->force_start = Qnil;
  1667.   XFASTINT (w->last_modified) = 0;
  1668.   windows_or_buffers_changed++;
  1669.   if (EQ (window, selected_window))
  1670.     Fset_buffer (buffer);
  1671.  
  1672.   return Qnil;
  1673. }
  1674.  
  1675. DEFUN ("select-window", Fselect_window, Sselect_window, 1, 1, 0,
  1676.   "Select WINDOW.  Most editing will apply to WINDOW's buffer.\n\
  1677. The main editor command loop selects the buffer of the selected window\n\
  1678. before each command.")
  1679.   (window)
  1680.      register Lisp_Object window;
  1681. {
  1682.   register struct window *w;
  1683.   register struct window *ow = XWINDOW (selected_window);
  1684.  
  1685.   CHECK_LIVE_WINDOW (window, 0);
  1686.  
  1687.   w = XWINDOW (window);
  1688.  
  1689.   if (NILP (w->buffer))
  1690.     error ("Trying to select deleted window or non-leaf window");
  1691.  
  1692.   XFASTINT (w->use_time) = ++window_select_count;
  1693.   if (EQ (window, selected_window))
  1694.     return window;
  1695.  
  1696.   Fset_marker (ow->pointm, make_number (BUF_PT (XBUFFER (ow->buffer))),
  1697.            ow->buffer);
  1698.  
  1699.   selected_window = window;
  1700. #ifdef MULTI_FRAME
  1701.   if (XFRAME (WINDOW_FRAME (w)) != selected_frame)
  1702.     {
  1703.       XFRAME (WINDOW_FRAME (w))->selected_window = window;
  1704.       /* Use this rather than Fhandle_switch_frame
  1705.      so that FRAME_FOCUS_FRAME is moved appropriately as we
  1706.      move around in the state where a minibuffer in a separate
  1707.      frame is active.  */
  1708.       Fselect_frame (WINDOW_FRAME (w), Qnil);
  1709.     }
  1710.   else
  1711.     selected_frame->selected_window = window;
  1712. #endif
  1713.  
  1714.   record_buffer (w->buffer);
  1715.   Fset_buffer (w->buffer);
  1716.  
  1717.   /* Go to the point recorded in the window.
  1718.      This is important when the buffer is in more
  1719.      than one window.  It also matters when
  1720.      redisplay_window has altered point after scrolling,
  1721.      because it makes the change only in the window.  */
  1722.   {
  1723.     register int new_point = marker_position (w->pointm);
  1724.     if (new_point < BEGV)
  1725.       SET_PT (BEGV);
  1726.     if (new_point > ZV)
  1727.       SET_PT (ZV);
  1728.     else
  1729.       SET_PT (new_point);
  1730.   }
  1731.  
  1732.   windows_or_buffers_changed++;
  1733.   return window;
  1734. }
  1735.  
  1736. DEFUN ("display-buffer", Fdisplay_buffer, Sdisplay_buffer, 1, 2,
  1737.        "BDisplay buffer: \nP",
  1738.   "Make BUFFER appear in some window but don't select it.\n\
  1739. BUFFER can be a buffer or a buffer name.\n\
  1740. If BUFFER is shown already in some window, just use that one,\n\
  1741. unless the window is the selected window and the optional second\n\
  1742. argument NOT-THIS-WINDOW is non-nil (interactively, with prefix arg).\n\
  1743. If `pop-up-frames' is non-nil, make a new frame if no window shows BUFFER.\n\
  1744. Returns the window displaying BUFFER.")
  1745.   (buffer, not_this_window)
  1746.      register Lisp_Object buffer, not_this_window;
  1747. {
  1748.   register Lisp_Object window, tem;
  1749.  
  1750.   buffer = Fget_buffer (buffer);
  1751.   CHECK_BUFFER (buffer, 0);
  1752.  
  1753.   if (!NILP (Vdisplay_buffer_function))
  1754.     return call2 (Vdisplay_buffer_function, buffer, not_this_window);
  1755.  
  1756.   if (NILP (not_this_window)
  1757.       && XBUFFER (XWINDOW (selected_window)->buffer) == XBUFFER (buffer))
  1758.     return selected_window;
  1759.  
  1760. #ifdef MULTI_FRAME
  1761.   /* If pop_up_frames,
  1762.      look for a window showing BUFFER on any visible frame.  */
  1763.   window = Fget_buffer_window (buffer, pop_up_frames ? Qvisible : Qnil);
  1764. #else
  1765.   window = Fget_buffer_window (buffer, Qnil);
  1766. #endif
  1767.   if (!NILP (window)
  1768.       && (NILP (not_this_window) || !EQ (window, selected_window)))
  1769.     return window;
  1770.  
  1771.   /* Certain buffer names get special handling.  */
  1772.   if (! NILP (Vspecial_display_function))
  1773.     {
  1774.       tem = Fmember (XBUFFER (buffer)->name, Vspecial_display_buffer_names);
  1775.       if (!NILP (tem))
  1776.     return call1 (Vspecial_display_function, buffer);
  1777.  
  1778.       for (tem = Vspecial_display_regexps; CONSP (tem); tem = XCONS (tem)->cdr)
  1779.     if (fast_string_match (XCONS (tem)->car, XBUFFER (buffer)->name) >= 0)
  1780.       return call1 (Vspecial_display_function, buffer);
  1781.     }
  1782.  
  1783. #ifdef MULTI_FRAME
  1784.   /* If there are no frames open that have more than a minibuffer,
  1785.      we need to create a new frame.  */
  1786.   if (pop_up_frames || last_nonminibuf_frame == 0)
  1787.     {
  1788.       window = Fframe_selected_window (call0 (Vpop_up_frame_function));
  1789.       Fset_window_buffer (window, buffer);
  1790.       return window;
  1791.     }
  1792. #endif /* MULTI_FRAME */
  1793.  
  1794.   if (pop_up_windows
  1795. #ifdef MULTI_FRAME
  1796.       || FRAME_MINIBUF_ONLY_P (selected_frame)
  1797. #endif
  1798.       )
  1799.     {
  1800.       Lisp_Object frames;
  1801.  
  1802.       frames = Qnil;      
  1803. #ifdef MULTI_FRAME
  1804.       if (FRAME_MINIBUF_ONLY_P (selected_frame))
  1805.     XSET (frames, Lisp_Frame, last_nonminibuf_frame);
  1806. #endif
  1807.       /* Don't try to create a window if would get an error */
  1808.       if (split_height_threshold < window_min_height << 1)
  1809.     split_height_threshold = window_min_height << 1;
  1810.  
  1811.       window = Fget_largest_window (frames);
  1812.  
  1813.       if (!NILP (window)
  1814.       && window_height (window) >= split_height_threshold
  1815.       && (XFASTINT (XWINDOW (window)->width)
  1816.           == FRAME_WIDTH (XFRAME (WINDOW_FRAME (XWINDOW (window))))))
  1817.     window = Fsplit_window (window, Qnil, Qnil);
  1818.       else
  1819.     {
  1820.       window = Fget_lru_window (frames);
  1821.       if ((EQ (window, selected_window)
  1822.            || EQ (XWINDOW (window)->parent, Qnil))
  1823.           && window_height (window) >= window_min_height << 1)
  1824.         window = Fsplit_window (window, Qnil, Qnil);
  1825.     }
  1826.     }
  1827.   else
  1828.     window = Fget_lru_window (Qnil);
  1829.  
  1830.   Fset_window_buffer (window, buffer);
  1831.   return window;
  1832. }
  1833.  
  1834. void
  1835. temp_output_buffer_show (buf)
  1836.      register Lisp_Object buf;
  1837. {
  1838.   register struct buffer *old = current_buffer;
  1839.   register Lisp_Object window;
  1840.   register struct window *w;
  1841.  
  1842.   Fset_buffer (buf);
  1843.   XBUFFER (buf)->save_modified = MODIFF;
  1844.   BEGV = BEG;
  1845.   ZV = Z;
  1846.   SET_PT (BEG);
  1847.   clip_changed = 1;
  1848.   set_buffer_internal (old);
  1849.  
  1850.   if (!EQ (Vtemp_buffer_show_function, Qnil))
  1851.     call1 (Vtemp_buffer_show_function, buf);
  1852.   else
  1853.     {
  1854.       window = Fdisplay_buffer (buf, Qnil);
  1855.  
  1856. #ifdef MULTI_FRAME
  1857.       if (XFRAME (XWINDOW (window)->frame) != selected_frame)
  1858.     Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
  1859. #endif /* MULTI_FRAME */
  1860.       Vminibuf_scroll_window = window;
  1861.       w = XWINDOW (window);
  1862.       XFASTINT (w->hscroll) = 0;
  1863.       set_marker_restricted (w->start, make_number (1), buf);
  1864.       set_marker_restricted (w->pointm, make_number (1), buf);
  1865.     }
  1866. }
  1867.  
  1868. static
  1869. make_dummy_parent (window)
  1870.      Lisp_Object window;
  1871. {
  1872.   register Lisp_Object old, new;
  1873.   register struct window *o, *p;
  1874.  
  1875.   old = window;
  1876.   XSETTYPE (old, Lisp_Vector);
  1877.   new = Fcopy_sequence (old);
  1878.   XSETTYPE (new, Lisp_Window);
  1879.  
  1880.   o = XWINDOW (old);
  1881.   p = XWINDOW (new);
  1882.   XFASTINT (p->sequence_number) = ++sequence_number;
  1883.  
  1884.   /* Put new into window structure in place of window */
  1885.   replace_window (window, new);
  1886.  
  1887.   o->next = Qnil;
  1888.   o->prev = Qnil;
  1889.   o->vchild = Qnil;
  1890.   o->hchild = Qnil;
  1891.   o->parent = new;
  1892.  
  1893.   p->start = Qnil;
  1894.   p->pointm = Qnil;
  1895.   p->buffer = Qnil;
  1896. }
  1897.  
  1898. DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
  1899.   "Split WINDOW, putting SIZE lines in the first of the pair.\n\
  1900. WINDOW defaults to selected one and SIZE to half its size.\n\
  1901. If optional third arg HOR-FLAG is non-nil, split side by side\n\
  1902. and put SIZE columns in the first of the pair.")
  1903.   (window, chsize, horflag)
  1904.      Lisp_Object window, chsize, horflag;
  1905. {
  1906.   register Lisp_Object new;
  1907.   register struct window *o, *p;
  1908.   register int size;
  1909.  
  1910.   if (NILP (window))
  1911.     window = selected_window;
  1912.   else
  1913.     CHECK_LIVE_WINDOW (window, 0);
  1914.  
  1915.   o = XWINDOW (window);
  1916.  
  1917.   if (NILP (chsize))
  1918.     {
  1919.       if (!NILP (horflag))
  1920.     /* Round odd size up, since this is for the left-hand window,
  1921.        and it will lose a column for the separators.  */
  1922.     size = ((XFASTINT (o->width) + 1) & -2) >> 1;
  1923.       else
  1924.     size = XFASTINT (o->height) >> 1;
  1925.     }
  1926.   else
  1927.     {
  1928.       CHECK_NUMBER (chsize, 1);
  1929.       size = XINT (chsize);
  1930.     }
  1931.  
  1932.   if (MINI_WINDOW_P (o))
  1933.     error ("Attempt to split minibuffer window");
  1934.   else if (FRAME_NO_SPLIT_P (XFRAME (WINDOW_FRAME (o))))
  1935.     error ("Attempt to split unsplittable frame");
  1936.  
  1937.   check_min_window_sizes ();
  1938.  
  1939.   if (NILP (horflag))
  1940.     {
  1941.       if (size < window_min_height
  1942.       || size + window_min_height > XFASTINT (o->height))
  1943.     args_out_of_range_3 (window, chsize, horflag);
  1944.       if (NILP (o->parent)
  1945.       || NILP (XWINDOW (o->parent)->vchild))
  1946.     {
  1947.       make_dummy_parent (window);
  1948.       new = o->parent;
  1949.       XWINDOW (new)->vchild = window;
  1950.     }
  1951.     }
  1952.   else
  1953.     {
  1954.       if (size < window_min_width
  1955.       || size + window_min_width > XFASTINT (o->width))
  1956.     args_out_of_range_3 (window, chsize, horflag);
  1957.       if (NILP (o->parent)
  1958.       || NILP (XWINDOW (o->parent)->hchild))
  1959.     {
  1960.       make_dummy_parent (window);
  1961.       new = o->parent;
  1962.       XWINDOW (new)->hchild = window;
  1963.     }
  1964.     }
  1965.  
  1966.   /* Now we know that window's parent is a vertical combination
  1967.      if we are dividing vertically, or a horizontal combination
  1968.      if we are making side-by-side windows */
  1969.  
  1970.   windows_or_buffers_changed++;
  1971.   new = make_window ();
  1972.   p = XWINDOW (new);
  1973.  
  1974.   p->frame = o->frame;
  1975.   p->next = o->next;
  1976.   if (!NILP (p->next))
  1977.     XWINDOW (p->next)->prev = new;
  1978.   p->prev = window;
  1979.   o->next = new;
  1980.   p->parent = o->parent;
  1981.   p->buffer = Qt;
  1982.  
  1983.   Fset_window_buffer (new, o->buffer);
  1984.  
  1985.   /* Apportion the available frame space among the two new windows */
  1986.  
  1987.   if (!NILP (horflag))
  1988.     {
  1989.       p->height = o->height;
  1990.       p->top = o->top;
  1991.       XFASTINT (p->width) = XFASTINT (o->width) - size;
  1992.       XFASTINT (o->width) = size;
  1993.       XFASTINT (p->left) = XFASTINT (o->left) + size;
  1994.     }
  1995.   else
  1996.     {
  1997.       p->left = o->left;
  1998.       p->width = o->width;
  1999.       XFASTINT (p->height) = XFASTINT (o->height) - size;
  2000.       XFASTINT (o->height) = size;
  2001.       XFASTINT (p->top) = XFASTINT (o->top) + size;
  2002.     }
  2003.  
  2004.   return new;
  2005. }
  2006.  
  2007. DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 2, "p",
  2008.   "Make current window ARG lines bigger.\n\
  2009. From program, optional second arg non-nil means grow sideways ARG columns.")
  2010.   (n, side)
  2011.      register Lisp_Object n, side;
  2012. {
  2013.   CHECK_NUMBER (n, 0);
  2014.   change_window_height (XINT (n), !NILP (side));
  2015.   return Qnil;
  2016. }
  2017.  
  2018. DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 2, "p",
  2019.   "Make current window ARG lines smaller.\n\
  2020. From program, optional second arg non-nil means shrink sideways ARG columns.")
  2021.   (n, side)
  2022.      register Lisp_Object n, side;
  2023. {
  2024.   CHECK_NUMBER (n, 0);
  2025.   change_window_height (-XINT (n), !NILP (side));
  2026.   return Qnil;
  2027. }
  2028.  
  2029. int
  2030. window_height (window)
  2031.      Lisp_Object window;
  2032. {
  2033.   register struct window *p = XWINDOW (window);
  2034.   return XFASTINT (p->height);
  2035. }
  2036.  
  2037. int
  2038. window_width (window)
  2039.      Lisp_Object window;
  2040. {
  2041.   register struct window *p = XWINDOW (window);
  2042.   return XFASTINT (p->width);
  2043. }
  2044.  
  2045. #define MINSIZE(w)                        \
  2046.   (widthflag                            \
  2047.    ? window_min_width                        \
  2048.    : (MINI_WINDOW_P (XWINDOW (w)) ? 1 : window_min_height))
  2049.  
  2050. #define CURBEG(w) \
  2051.   *(widthflag ? (int *) &(XWINDOW (w)->left) : (int *) &(XWINDOW (w)->top))
  2052.  
  2053. #define CURSIZE(w) \
  2054.   *(widthflag ? (int *) &(XWINDOW (w)->width) : (int *) &(XWINDOW (w)->height))
  2055.  
  2056. /* Unlike set_window_height, this function
  2057.    also changes the heights of the siblings so as to
  2058.    keep everything consistent. */
  2059.  
  2060. change_window_height (delta, widthflag)
  2061.      register int delta;
  2062.      int widthflag;
  2063. {
  2064.   register Lisp_Object parent;
  2065.   Lisp_Object window;
  2066.   register struct window *p;
  2067.   int *sizep;
  2068.   int (*sizefun) () = widthflag ? window_width : window_height;
  2069.   register int (*setsizefun) () = (widthflag
  2070.                    ? set_window_width
  2071.                    : set_window_height);
  2072.  
  2073.   check_min_window_sizes ();
  2074.  
  2075.   window = selected_window;
  2076.   while (1)
  2077.     {
  2078.       p = XWINDOW (window);
  2079.       parent = p->parent;
  2080.       if (NILP (parent))
  2081.     {
  2082.       if (widthflag)
  2083.         error ("No other window to side of this one");
  2084.       break;
  2085.     }
  2086.       if (widthflag ? !NILP (XWINDOW (parent)->hchild)
  2087.       : !NILP (XWINDOW (parent)->vchild))
  2088.     break;
  2089.       window = parent;
  2090.     }
  2091.  
  2092.   sizep = &CURSIZE (window);
  2093.  
  2094.   {
  2095.     register int maxdelta;
  2096.  
  2097.     maxdelta = (!NILP (parent) ? (*sizefun) (parent) - *sizep
  2098.         : !NILP (p->next) ? (*sizefun) (p->next) - MINSIZE (p->next)
  2099.         : !NILP (p->prev) ? (*sizefun) (p->prev) - MINSIZE (p->prev)
  2100.         /* This is a frame with only one window, a minibuffer-only
  2101.            or a minibufferless frame.  */
  2102.         : (delta = 0));
  2103.  
  2104.     if (delta > maxdelta)
  2105.       /* This case traps trying to make the minibuffer
  2106.      the full frame, or make the only window aside from the
  2107.      minibuffer the full frame.  */
  2108.       delta = maxdelta;
  2109.   }
  2110.  
  2111.   if (*sizep + delta < MINSIZE (window))
  2112.     {
  2113.       Fdelete_window (window);
  2114.       return;
  2115.     }
  2116.  
  2117.   if (delta == 0)
  2118.     return;
  2119.  
  2120.   if (!NILP (p->next)
  2121.       && (*sizefun) (p->next) - delta >= MINSIZE (p->next))
  2122.     {
  2123.       (*setsizefun) (p->next, (*sizefun) (p->next) - delta, 0);
  2124.       (*setsizefun) (window, *sizep + delta, 0);
  2125.       CURBEG (p->next) += delta;
  2126.       /* This does not change size of p->next,
  2127.      but it propagates the new top edge to its children */
  2128.       (*setsizefun) (p->next, (*sizefun) (p->next), 0);
  2129.     }
  2130.   else if (!NILP (p->prev)
  2131.        && (*sizefun) (p->prev) - delta >= MINSIZE (p->prev))
  2132.     {
  2133.       (*setsizefun) (p->prev, (*sizefun) (p->prev) - delta, 0);
  2134.       CURBEG (window) -= delta;
  2135.       (*setsizefun) (window, *sizep + delta, 0);
  2136.     }
  2137.   else
  2138.     {
  2139.       register int delta1;
  2140.       register int opht = (*sizefun) (parent);
  2141.  
  2142.       /* If trying to grow this window to or beyond size of the parent,
  2143.      make delta1 so big that, on shrinking back down,
  2144.      all the siblings end up with less than one line and are deleted.  */
  2145.       if (opht <= *sizep + delta)
  2146.     delta1 = opht * opht * 2;
  2147.       /* Otherwise, make delta1 just right so that if we add delta1
  2148.      lines to this window and to the parent, and then shrink
  2149.      the parent back to its original size, the new proportional
  2150.      size of this window will increase by delta.  */
  2151.       else
  2152.     delta1 = (delta * opht * 100) / ((opht - *sizep - delta) * 100);
  2153.  
  2154.       /* Add delta1 lines or columns to this window, and to the parent,
  2155.      keeping things consistent while not affecting siblings.  */
  2156.       CURSIZE (parent) = opht + delta1;
  2157.       (*setsizefun) (window, *sizep + delta1, 0);
  2158.  
  2159.       /* Squeeze out delta1 lines or columns from our parent,
  2160.      shriking this window and siblings proportionately.
  2161.      This brings parent back to correct size.
  2162.      Delta1 was calculated so this makes this window the desired size,
  2163.      taking it all out of the siblings.  */
  2164.       (*setsizefun) (parent, opht, 0);
  2165.     }
  2166.  
  2167.   XFASTINT (p->last_modified) = 0;
  2168. }
  2169. #undef MINSIZE
  2170. #undef CURBEG
  2171. #undef CURSIZE
  2172.  
  2173.  
  2174. /* Return number of lines of text (not counting mode line) in W.  */
  2175.  
  2176. int
  2177. window_internal_height (w)
  2178.      struct window *w;
  2179. {
  2180.   int ht = XFASTINT (w->height);
  2181.  
  2182.   if (MINI_WINDOW_P (w))
  2183.     return ht;
  2184.  
  2185.   if (!NILP (w->parent) || !NILP (w->vchild) || !NILP (w->hchild)
  2186.       || !NILP (w->next) || !NILP (w->prev)
  2187.       || FRAME_WANTS_MODELINE_P (XFRAME (WINDOW_FRAME (w))))
  2188.     return ht - 1;
  2189.  
  2190.   return ht;
  2191. }
  2192.  
  2193.  
  2194. /* Return the number of columns in W.
  2195.    Don't count columns occupied by scroll bars or the vertical bar
  2196.    separating W from the sibling to its right.  */
  2197. int
  2198. window_internal_width (w)
  2199.      struct window *w;
  2200. {
  2201.   FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
  2202.   int left = XINT (w->left);
  2203.   int width = XINT (w->width);
  2204.  
  2205.   /* If this window is flush against the right edge of the frame, its
  2206.      internal width is its full width.  */
  2207.   if (left + width >= FRAME_WIDTH (f))
  2208.     return width;
  2209.  
  2210.   /* If we are not flush right, then our rightmost columns are
  2211.      occupied by some sort of separator.  */
  2212.  
  2213.   /* Scroll bars occupy a few columns.  */
  2214.   if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
  2215.     return width - VERTICAL_SCROLL_BAR_WIDTH;
  2216.  
  2217.   /* The column of `|' characters separating side-by-side windows
  2218.      occupies one column only.  */
  2219.   return width - 1;
  2220. }
  2221.  
  2222.  
  2223. /* Scroll contents of window WINDOW up N lines.  */
  2224.  
  2225. void
  2226. window_scroll (window, n, noerror)
  2227.      Lisp_Object window;
  2228.      int n;
  2229.      int noerror;
  2230. {
  2231.   register struct window *w = XWINDOW (window);
  2232.   register int opoint = PT;
  2233.   register int pos;
  2234.   register int ht = window_internal_height (w);
  2235.   register Lisp_Object tem;
  2236.   int lose;
  2237.   Lisp_Object bolp, nmoved;
  2238.  
  2239.   XFASTINT (tem) = PT;
  2240.   tem = Fpos_visible_in_window_p (tem, window);
  2241.  
  2242.   if (NILP (tem))
  2243.     {
  2244.       Fvertical_motion (make_number (- (ht / 2)), window);
  2245.       XFASTINT (tem) = PT;
  2246.       Fset_marker (w->start, tem, w->buffer);
  2247.       w->force_start = Qt;
  2248.     }
  2249.  
  2250.   SET_PT (marker_position (w->start));
  2251.   lose = n < 0 && PT == BEGV;
  2252.   Fvertical_motion (make_number (n), window);
  2253.   pos = PT;
  2254.   bolp = Fbolp ();
  2255.   SET_PT (opoint);
  2256.  
  2257.   if (lose)
  2258.     {
  2259.       if (noerror)
  2260.     return;
  2261.       else
  2262.     Fsignal (Qbeginning_of_buffer, Qnil);
  2263.     }
  2264.  
  2265.   if (pos < ZV)
  2266.     {
  2267.       set_marker_restricted (w->start, make_number (pos), w->buffer);
  2268.       w->start_at_line_beg = bolp;
  2269.       w->update_mode_line = Qt;
  2270.       XFASTINT (w->last_modified) = 0;
  2271.       if (pos > opoint)
  2272.     SET_PT (pos);
  2273.       if (n < 0)
  2274.     {
  2275.       SET_PT (pos);
  2276.       tem = Fvertical_motion (make_number (ht), window);
  2277.       if (PT > opoint || XFASTINT (tem) < ht)
  2278.         SET_PT (opoint);
  2279.       else
  2280.         Fvertical_motion (make_number (-1), window);
  2281.     }
  2282.     }
  2283.   else
  2284.     {
  2285.       if (noerror)
  2286.     return;
  2287.       else
  2288.     Fsignal (Qend_of_buffer, Qnil);
  2289.     }
  2290. }
  2291.  
  2292. /* This is the guts of Fscroll_up and Fscroll_down.  */
  2293.  
  2294. static void
  2295. scroll_command (n, direction)
  2296.      register Lisp_Object n;
  2297.      int direction;
  2298. {
  2299.   register int defalt;
  2300.   int count = specpdl_ptr - specpdl;
  2301.  
  2302.   /* If selected window's buffer isn't current, make it current for the moment.
  2303.      But don't screw up if window_scroll gets an error.  */
  2304.   if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
  2305.     {
  2306.       record_unwind_protect (save_excursion_restore, save_excursion_save ());
  2307.       Fset_buffer (XWINDOW (selected_window)->buffer);
  2308.     }
  2309.  
  2310.   defalt = (window_internal_height (XWINDOW (selected_window))
  2311.         - next_screen_context_lines);
  2312.   defalt = direction * (defalt < 1 ? 1 : defalt);
  2313.  
  2314.   if (NILP (n))
  2315.     window_scroll (selected_window, defalt, 0);
  2316.   else if (EQ (n, Qminus))
  2317.     window_scroll (selected_window, - defalt, 0);
  2318.   else
  2319.     {
  2320.       n = Fprefix_numeric_value (n);
  2321.       window_scroll (selected_window, XINT (n) * direction, 0);
  2322.     }
  2323.  
  2324.   unbind_to (count, Qnil);
  2325. }
  2326.  
  2327. DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "P",
  2328.   "Scroll text of current window upward ARG lines; or near full screen if no ARG.\n\
  2329. A near full screen is `next-screen-context-lines' less than a full screen.\n\
  2330. Negative ARG means scroll downward.\n\
  2331. When calling from a program, supply a number as argument or nil.")
  2332.   (n)
  2333.      Lisp_Object n;
  2334. {
  2335.   scroll_command (n, 1);
  2336.   return Qnil;
  2337. }
  2338.  
  2339. DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "P",
  2340.   "Scroll text of current window downward ARG lines; or near full screen if no ARG.\n\
  2341. A near full screen is `next-screen-context-lines' less than a full screen.\n\
  2342. Negative ARG means scroll upward.\n\
  2343. When calling from a program, supply a number as argument or nil.")
  2344.   (n)
  2345.      Lisp_Object n;
  2346. {
  2347.   scroll_command (n, -1);
  2348.   return Qnil;
  2349. }
  2350.  
  2351. DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
  2352.   "Scroll next window upward ARG lines; or near full screen if no ARG.\n\
  2353. The next window is the one below the current one; or the one at the top\n\
  2354. if the current one is at the bottom.  Negative ARG means scroll downward.\n\
  2355. When calling from a program, supply a number as argument or nil.\n\
  2356. \n\
  2357. If in the minibuffer, `minibuffer-scroll-window' if non-nil\n\
  2358. specifies the window to scroll.\n\
  2359. If `other-window-scroll-buffer' is non-nil, scroll the window\n\
  2360. showing that buffer, popping the buffer up if necessary.")
  2361.   (n)
  2362.      register Lisp_Object n;
  2363. {
  2364.   register Lisp_Object window;
  2365.   register int ht;
  2366.   register struct window *w;
  2367.   register int count = specpdl_ptr - specpdl;
  2368.  
  2369.   if (MINI_WINDOW_P (XWINDOW (selected_window))
  2370.       && !NILP (Vminibuf_scroll_window))
  2371.     window = Vminibuf_scroll_window;
  2372.   /* If buffer is specified, scroll that buffer.  */
  2373.   else if (!NILP (Vother_window_scroll_buffer))
  2374.     {
  2375.       window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
  2376.       if (NILP (window))
  2377.     window = Fdisplay_buffer (Vother_window_scroll_buffer, Qt);
  2378.     }
  2379.   else
  2380.     {
  2381.       /* Nothing specified; look for a neighboring window on the same
  2382.      frame.  */
  2383.       window = Fnext_window (selected_window, Qnil, Qnil);
  2384.  
  2385.       if (EQ (window, selected_window))
  2386.     /* That didn't get us anywhere; look for a window on another
  2387.            visible frame.  */
  2388.     do
  2389.       window = Fnext_window (window, Qnil, Qt);
  2390.     while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
  2391.            && ! EQ (window, selected_window));
  2392.     }
  2393.  
  2394.   CHECK_LIVE_WINDOW (window, 0);
  2395.  
  2396.   if (EQ (window, selected_window))
  2397.     error ("There is no other window");
  2398.  
  2399.   w = XWINDOW (window);
  2400.   ht = window_internal_height (w);
  2401.  
  2402.   /* Don't screw up if window_scroll gets an error.  */
  2403.   record_unwind_protect (save_excursion_restore, save_excursion_save ());
  2404.  
  2405.   Fset_buffer (w->buffer);
  2406.   SET_PT (marker_position (w->pointm));
  2407.  
  2408.   if (NILP (n))
  2409.     window_scroll (window, ht - next_screen_context_lines, 1);
  2410.   else if (EQ (n, Qminus))
  2411.     window_scroll (window, next_screen_context_lines - ht, 1);
  2412.   else
  2413.     {
  2414.       if (XTYPE (n) == Lisp_Cons)
  2415.     n = Fcar (n);
  2416.       CHECK_NUMBER (n, 0);
  2417.       window_scroll (window, XINT (n), 1);
  2418.     }
  2419.  
  2420.   Fset_marker (w->pointm, make_number (PT), Qnil);
  2421.   unbind_to (count, Qnil);
  2422.  
  2423.   return Qnil;
  2424. }
  2425.  
  2426. DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 1, "P",
  2427.   "Scroll selected window display ARG columns left.\n\
  2428. Default for ARG is window width minus 2.")
  2429.   (arg)
  2430.      register Lisp_Object arg;
  2431. {
  2432.  
  2433.   if (NILP (arg))
  2434.     XFASTINT (arg) = window_internal_width (XWINDOW (selected_window)) - 2;
  2435.   else
  2436.     arg = Fprefix_numeric_value (arg);
  2437.  
  2438.   return
  2439.     Fset_window_hscroll (selected_window,
  2440.              make_number (XINT (XWINDOW (selected_window)->hscroll)
  2441.                       + XINT (arg)));
  2442. }
  2443.  
  2444. DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 1, "P",
  2445.   "Scroll selected window display ARG columns right.\n\
  2446. Default for ARG is window width minus 2.")
  2447.   (arg)
  2448.      register Lisp_Object arg;
  2449. {
  2450.   if (NILP (arg))
  2451.     XFASTINT (arg) = window_internal_width (XWINDOW (selected_window)) - 2;
  2452.   else
  2453.     arg = Fprefix_numeric_value (arg);
  2454.  
  2455.   return
  2456.     Fset_window_hscroll (selected_window,
  2457.              make_number (XINT (XWINDOW (selected_window)->hscroll)
  2458.                       - XINT (arg)));
  2459. }
  2460.  
  2461. DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
  2462.   "Center point in window and redisplay frame.  With ARG, put point on line ARG.\n\
  2463. The desired position of point is always relative to the current window.\n\
  2464. Just C-u as prefix means put point in the center of the window.\n\
  2465. No arg (i.e., it is nil) erases the entire frame and then\n\
  2466. redraws with point in the center of the current window.")
  2467.   (n)
  2468.      register Lisp_Object n;
  2469. {
  2470.   register struct window *w = XWINDOW (selected_window);
  2471.   register int ht = window_internal_height (w);
  2472.   register int opoint = PT;
  2473.   Lisp_Object window;
  2474.  
  2475.   if (NILP (n))
  2476.     {
  2477.       extern int frame_garbaged;
  2478.  
  2479.       SET_FRAME_GARBAGED (XFRAME (WINDOW_FRAME (w)));
  2480.       XFASTINT (n) = ht / 2;
  2481.     }
  2482.   else if (XTYPE (n) == Lisp_Cons) /* Just C-u. */
  2483.     {
  2484.       XFASTINT (n) = ht / 2;
  2485.     }
  2486.   else
  2487.     {
  2488.       n = Fprefix_numeric_value (n);
  2489.       CHECK_NUMBER (n, 0);
  2490.     }
  2491.  
  2492.   if (XINT (n) < 0)
  2493.     XSETINT (n, XINT (n) + ht);
  2494.  
  2495.   XSETINT (n, - XINT (n));
  2496.  
  2497.   XSET (window, Lisp_Window, w);
  2498.   Fvertical_motion (n, window);
  2499.   Fset_marker (w->start, make_number (PT), w->buffer);
  2500.   w->start_at_line_beg = Fbolp ();
  2501.  
  2502.   SET_PT (opoint);
  2503.   w->force_start = Qt;
  2504.  
  2505.   return Qnil;
  2506. }
  2507.  
  2508. DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
  2509.   1, 1, "P",
  2510.   "Position point relative to window.\n\
  2511. With no argument, position text at center of window.\n\
  2512. An argument specifies frame line; zero means top of window,\n\
  2513. negative means relative to bottom of window.")
  2514.   (arg)
  2515.      register Lisp_Object arg;
  2516. {
  2517.   register struct window *w = XWINDOW (selected_window);
  2518.   register int height = window_internal_height (w);
  2519.   register int start;
  2520.   Lisp_Object window;
  2521.  
  2522.   if (NILP (arg))
  2523.     XFASTINT (arg) = height / 2;
  2524.   else
  2525.     {
  2526.       arg = Fprefix_numeric_value (arg);
  2527.       if (XINT (arg) < 0)
  2528.     XSETINT (arg, XINT (arg) + height);
  2529.     }
  2530.  
  2531.   start = marker_position (w->start);
  2532.   XSET (window, Lisp_Window, w);
  2533.   if (start < BEGV || start > ZV)
  2534.     {
  2535.       Fvertical_motion (make_number (- (height / 2)), window);
  2536.       Fset_marker (w->start, make_number (PT), w->buffer);
  2537.       w->start_at_line_beg = Fbolp ();
  2538.       w->force_start = Qt;
  2539.     }
  2540.   else
  2541.     SET_PT (start);
  2542.  
  2543.   return Fvertical_motion (arg, window);
  2544. }
  2545.  
  2546. struct save_window_data
  2547.   {
  2548.     int size_from_Lisp_Vector_struct;
  2549.     struct Lisp_Vector *next_from_Lisp_Vector_struct;
  2550.     Lisp_Object frame_width, frame_height, frame_menu_bar_lines;
  2551.     Lisp_Object selected_frame;
  2552.     Lisp_Object current_window;
  2553.     Lisp_Object current_buffer;
  2554.     Lisp_Object minibuf_scroll_window;
  2555.     Lisp_Object root_window;
  2556.     Lisp_Object focus_frame;
  2557.     /* A vector, interpreted as a struct saved_window */
  2558.     Lisp_Object saved_windows;
  2559.   };
  2560.  
  2561. /* Arg to Fmake_vector */
  2562. #define SAVE_WINDOW_DATA_SIZE                        \
  2563.   ((sizeof (struct save_window_data)                    \
  2564.     - (sizeof (struct Lisp_Vector)                    \
  2565.        /* Don't count the contents member of the struct Lisp_Vector */    \
  2566.        - sizeof (Lisp_Object)))                        \
  2567.    / sizeof (Lisp_Object))
  2568.  
  2569. /* This is saved as a Lisp_Vector */
  2570. struct saved_window
  2571.   {
  2572.     /* these first two must agree with struct Lisp_Vector in lisp.h */
  2573.     int size_from_Lisp_Vector_struct;
  2574.     struct Lisp_Vector *next_from_Lisp_Vector_struct;
  2575.  
  2576.     Lisp_Object window;
  2577.     Lisp_Object buffer, start, pointm, mark;
  2578.     Lisp_Object left, top, width, height, hscroll;
  2579.     Lisp_Object parent, prev;
  2580.     Lisp_Object start_at_line_beg;
  2581.     Lisp_Object display_table;
  2582.   };
  2583. #define SAVED_WINDOW_VECTOR_SIZE 14 /* Arg to Fmake_vector */
  2584.  
  2585. #define SAVED_WINDOW_N(swv,n) \
  2586.   ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
  2587.  
  2588. DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
  2589.   "T if OBJECT is a window-configration object.")
  2590.   (obj)
  2591.      Lisp_Object obj;
  2592. {
  2593.   if (XTYPE (obj) == Lisp_Window_Configuration)
  2594.     return Qt;
  2595.   return Qnil;
  2596. }
  2597.  
  2598.  
  2599. DEFUN ("set-window-configuration", Fset_window_configuration,
  2600.   Sset_window_configuration, 1, 1, 0,
  2601.   "Set the configuration of windows and buffers as specified by CONFIGURATION.\n\
  2602. CONFIGURATION must be a value previously returned\n\
  2603. by `current-window-configuration' (which see).")
  2604.      (configuration)
  2605.      Lisp_Object configuration;
  2606. {
  2607.   register struct save_window_data *data;
  2608.   struct Lisp_Vector *saved_windows;
  2609.   Lisp_Object new_current_buffer;
  2610.   Lisp_Object frame;
  2611.   FRAME_PTR f;
  2612.  
  2613.   while (XTYPE (configuration) != Lisp_Window_Configuration)
  2614.     {
  2615.       configuration = wrong_type_argument (intern ("window-configuration-p"),
  2616.                        configuration);
  2617.     }
  2618.  
  2619.   data = (struct save_window_data *) XVECTOR (configuration);
  2620.   saved_windows = XVECTOR (data->saved_windows);
  2621.  
  2622.   new_current_buffer = data->current_buffer;
  2623.   if (NILP (XBUFFER (new_current_buffer)->name))
  2624.     new_current_buffer = Qnil;
  2625.  
  2626.   frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
  2627.   f = XFRAME (frame);
  2628.  
  2629.   /* If f is a dead frame, don't bother rebuilding its window tree.
  2630.      However, there is other stuff we should still try to do below.  */
  2631.   if (FRAME_LIVE_P (f))
  2632.     {
  2633.       register struct window *w;
  2634.       register struct saved_window *p;
  2635.       int k;
  2636.  
  2637.       /* If the frame has been resized since this window configuration was
  2638.      made, we change the frame to the size specified in the
  2639.      configuration, restore the configuration, and then resize it
  2640.      back.  We keep track of the prevailing height in these variables.  */
  2641.       int previous_frame_height = FRAME_HEIGHT (f);
  2642.       int previous_frame_width =  FRAME_WIDTH  (f);
  2643.       int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
  2644.  
  2645.       if (XFASTINT (data->frame_height) != previous_frame_height
  2646.       || XFASTINT (data->frame_width) != previous_frame_width)
  2647.     change_frame_size (f, data->frame_height, data->frame_width, 0, 0);
  2648. #if defined(HAVE_X_WINDOWS) || defined(USE_EXTERNAL_MENU_BAR)
  2649.       if (XFASTINT (data->frame_menu_bar_lines)
  2650.       != previous_frame_menu_bar_lines)
  2651.     x_set_menu_bar_lines (f, data->frame_menu_bar_lines, 0);
  2652. #endif
  2653.  
  2654.       windows_or_buffers_changed++;
  2655.  
  2656.       /* Kludge Alert!
  2657.      Mark all windows now on frame as "deleted".
  2658.      Restoring the new configuration "undeletes" any that are in it.
  2659.      
  2660.      Save their current buffers in their height fields, since we may
  2661.      need it later, if a buffer saved in the configuration is now
  2662.      dead.  */
  2663.       delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
  2664.  
  2665.       for (k = 0; k < saved_windows->size; k++)
  2666.     {
  2667.       p = SAVED_WINDOW_N (saved_windows, k);
  2668.       w = XWINDOW (p->window);
  2669.       w->next = Qnil;
  2670.  
  2671.       if (!NILP (p->parent))
  2672.         w->parent = SAVED_WINDOW_N (saved_windows,
  2673.                     XFASTINT (p->parent))->window;
  2674.       else
  2675.         w->parent = Qnil;
  2676.  
  2677.       if (!NILP (p->prev))
  2678.         {
  2679.           w->prev = SAVED_WINDOW_N (saved_windows,
  2680.                     XFASTINT (p->prev))->window;
  2681.           XWINDOW (w->prev)->next = p->window;
  2682.         }
  2683.       else
  2684.         {
  2685.           w->prev = Qnil;
  2686.           if (!NILP (w->parent))
  2687.         {
  2688.           if (EQ (p->width, XWINDOW (w->parent)->width))
  2689.             {
  2690.               XWINDOW (w->parent)->vchild = p->window;
  2691.               XWINDOW (w->parent)->hchild = Qnil;
  2692.             }
  2693.           else
  2694.             {
  2695.               XWINDOW (w->parent)->hchild = p->window;
  2696.               XWINDOW (w->parent)->vchild = Qnil;
  2697.             }
  2698.         }
  2699.         }
  2700.  
  2701.       /* If we squirreled away the buffer in the window's height,
  2702.          restore it now.  */
  2703.       if (XTYPE (w->height) == Lisp_Buffer)
  2704.         w->buffer = w->height;
  2705.       w->left = p->left;
  2706.       w->top = p->top;
  2707.       w->width = p->width;
  2708.       w->height = p->height;
  2709.       w->hscroll = p->hscroll;
  2710.       w->display_table = p->display_table;
  2711.       XFASTINT (w->last_modified) = 0;
  2712.  
  2713.       /* Reinstall the saved buffer and pointers into it.  */
  2714.       if (NILP (p->buffer))
  2715.         w->buffer = p->buffer;
  2716.       else
  2717.         {
  2718.           if (!NILP (XBUFFER (p->buffer)->name))
  2719.         /* If saved buffer is alive, install it.  */
  2720.         {
  2721.           w->buffer = p->buffer;
  2722.           w->start_at_line_beg = p->start_at_line_beg;
  2723.           set_marker_restricted (w->start,
  2724.                      Fmarker_position (p->start),
  2725.                      w->buffer);
  2726.           set_marker_restricted (w->pointm,
  2727.                      Fmarker_position (p->pointm),
  2728.                      w->buffer);
  2729.           Fset_marker (XBUFFER (w->buffer)->mark,
  2730.                    Fmarker_position (p->mark), w->buffer);
  2731.  
  2732.           /* As documented in Fcurrent_window_configuration, don't
  2733.              save the location of point in the buffer which was current
  2734.              when the window configuration was recorded.  */
  2735.           if (!EQ (p->buffer, new_current_buffer)
  2736.               && XBUFFER (p->buffer) == current_buffer)
  2737.             Fgoto_char (w->pointm);
  2738.         }
  2739.           else if (NILP (w->buffer) || NILP (XBUFFER (w->buffer)->name))
  2740.         /* Else unless window has a live buffer, get one.  */
  2741.         {
  2742.           w->buffer = Fcdr (Fcar (Vbuffer_alist));
  2743.           /* This will set the markers to beginning of visible
  2744.              range.  */
  2745.           set_marker_restricted (w->start, make_number (0), w->buffer);
  2746.           set_marker_restricted (w->pointm, make_number (0),w->buffer);
  2747.           w->start_at_line_beg = Qt;
  2748.         }
  2749.           else
  2750.         /* Keeping window's old buffer; make sure the markers
  2751.            are real.  */
  2752.         {
  2753.           /* Set window markers at start of visible range.  */
  2754.           if (XMARKER (w->start)->buffer == 0)
  2755.             set_marker_restricted (w->start, make_number (0),
  2756.                        w->buffer);
  2757.           if (XMARKER (w->pointm)->buffer == 0)
  2758.             set_marker_restricted (w->pointm,
  2759.                        (make_number
  2760.                         (BUF_PT (XBUFFER (w->buffer)))),
  2761.                        w->buffer);
  2762.           w->start_at_line_beg = Qt;
  2763.         }
  2764.         }
  2765.     }
  2766.  
  2767.       FRAME_ROOT_WINDOW (f) = data->root_window;
  2768.       Fselect_window (data->current_window);
  2769.  
  2770. #ifdef MULTI_FRAME
  2771.       if (NILP (data->focus_frame)
  2772.       || (XTYPE (data->focus_frame) == Lisp_Frame
  2773.           && FRAME_LIVE_P (XFRAME (data->focus_frame))))
  2774.     Fredirect_frame_focus (frame, data->focus_frame);
  2775. #endif
  2776.  
  2777. #if 0 /* I don't understand why this is needed, and it causes problems
  2778.          when the frame's old selected window has been deleted.  */
  2779. #ifdef MULTI_FRAME
  2780.       if (f != selected_frame && ! FRAME_TERMCAP_P (f))
  2781.     Fhandle_switch_frame (WINDOW_FRAME (XWINDOW (data->root_window)), Qnil);
  2782. #endif
  2783. #endif
  2784.  
  2785.       /* Set the screen height to the value it had before this function.  */
  2786.       if (previous_frame_height != FRAME_HEIGHT (f)
  2787.       || previous_frame_width != FRAME_WIDTH (f))
  2788.     change_frame_size (f, previous_frame_height, previous_frame_width,
  2789.                0, 0);
  2790. #ifdef defined(HAVE_X_WINDOWS) || defined(USE_EXTERNAL_MENU_BAR) /* CHFIXME */
  2791.       if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
  2792.     x_set_menu_bar_lines (f, previous_frame_menu_bar_lines, 0);
  2793. #endif
  2794.     }
  2795.  
  2796. #ifdef MULTI_FRAME
  2797.   /* Fselect_window will have made f the selected frame, so we
  2798.      reselect the proper frame here.  Fhandle_switch_frame will change the
  2799.      selected window too, but that doesn't make the call to
  2800.      Fselect_window above totally superfluous; it still sets f's
  2801.      selected window.  */
  2802.   if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
  2803.     Fhandle_switch_frame (data->selected_frame, Qnil);
  2804. #endif
  2805.  
  2806.   if (!NILP (new_current_buffer))
  2807.     Fset_buffer (new_current_buffer);
  2808.  
  2809.   Vminibuf_scroll_window = data->minibuf_scroll_window;
  2810.   return (Qnil);
  2811. }
  2812.  
  2813. /* Mark all windows now on frame as deleted
  2814.    by setting their buffers to nil.  */
  2815.  
  2816. void
  2817. delete_all_subwindows (w)
  2818.      register struct window *w;
  2819. {
  2820.   if (!NILP (w->next))
  2821.     delete_all_subwindows (XWINDOW (w->next));
  2822.   if (!NILP (w->vchild))
  2823.     delete_all_subwindows (XWINDOW (w->vchild));
  2824.   if (!NILP (w->hchild))
  2825.     delete_all_subwindows (XWINDOW (w->hchild));
  2826.  
  2827.   w->height = w->buffer;       /* See Fset_window_configuration for excuse.  */
  2828.  
  2829.   /* We set all three of these fields to nil, to make sure that we can
  2830.      distinguish this dead window from any live window.  Live leaf
  2831.      windows will have buffer set, and combination windows will have
  2832.      vchild or hchild set.  */
  2833.   w->buffer = Qnil;
  2834.   w->vchild = Qnil;
  2835.   w->hchild = Qnil;
  2836. }
  2837.  
  2838. static int
  2839. count_windows (window)
  2840.      register struct window *window;
  2841. {
  2842.   register int count = 1;
  2843.   if (!NILP (window->next))
  2844.     count += count_windows (XWINDOW (window->next));
  2845.   if (!NILP (window->vchild))
  2846.     count += count_windows (XWINDOW (window->vchild));
  2847.   if (!NILP (window->hchild))
  2848.     count += count_windows (XWINDOW (window->hchild));
  2849.   return count;
  2850. }
  2851.  
  2852. static int
  2853. save_window_save (window, vector, i)
  2854.      Lisp_Object window;
  2855.      struct Lisp_Vector *vector;
  2856.      int i;
  2857. {
  2858.   register struct saved_window *p;
  2859.   register struct window *w;
  2860.   register Lisp_Object tem;
  2861.  
  2862.   for (;!NILP (window); window = w->next)
  2863.     {
  2864.       p = SAVED_WINDOW_N (vector, i);
  2865.       w = XWINDOW (window);
  2866.  
  2867.       XFASTINT (w->temslot) = i++;
  2868.       p->window = window;
  2869.       p->buffer = w->buffer;
  2870.       p->left = w->left;
  2871.       p->top = w->top;
  2872.       p->width = w->width;
  2873.       p->height = w->height;
  2874.       p->hscroll = w->hscroll;
  2875.       p->display_table = w->display_table;
  2876.       if (!NILP (w->buffer))
  2877.     {
  2878.       /* Save w's value of point in the window configuration.
  2879.          If w is the selected window, then get the value of point
  2880.          from the buffer; pointm is garbage in the selected window.  */
  2881.       if (EQ (window, selected_window))
  2882.         {
  2883.           p->pointm = Fmake_marker ();
  2884.           Fset_marker (p->pointm, BUF_PT (XBUFFER (w->buffer)),
  2885.                w->buffer);
  2886.         }
  2887.       else
  2888.         p->pointm = Fcopy_marker (w->pointm);
  2889.  
  2890.       p->start = Fcopy_marker (w->start);
  2891.       p->start_at_line_beg = w->start_at_line_beg;
  2892.  
  2893.       tem = XBUFFER (w->buffer)->mark;
  2894.       p->mark = Fcopy_marker (tem);
  2895.     }
  2896.       else
  2897.     {
  2898.       p->pointm = Qnil;
  2899.       p->start = Qnil;
  2900.       p->mark = Qnil;
  2901.       p->start_at_line_beg = Qnil;
  2902.     }
  2903.  
  2904.       if (NILP (w->parent))
  2905.     p->parent = Qnil;
  2906.       else
  2907.     p->parent = XWINDOW (w->parent)->temslot;
  2908.  
  2909.       if (NILP (w->prev))
  2910.     p->prev = Qnil;
  2911.       else
  2912.     p->prev = XWINDOW (w->prev)->temslot;
  2913.  
  2914.       if (!NILP (w->vchild))
  2915.     i = save_window_save (w->vchild, vector, i);
  2916.       if (!NILP (w->hchild))
  2917.     i = save_window_save (w->hchild, vector, i);
  2918.     }
  2919.  
  2920.   return i;
  2921. }
  2922.  
  2923. DEFUN ("current-window-configuration",
  2924.     Fcurrent_window_configuration, Scurrent_window_configuration, 0, 1, 0,
  2925.   "Return an object representing the current window configuration of FRAME.\n\
  2926. If FRAME is nil or omitted, use the selected frame.\n\
  2927. This describes the number of windows, their sizes and current buffers,\n\
  2928. and for each displayed buffer, where display starts, and the positions of\n\
  2929. point and mark.  An exception is made for point in the current buffer:\n\
  2930. its value is -not- saved.\n\
  2931. This also records the currently selected frame, and FRAME's focus\n\
  2932. redirection (see `redirect-frame-focus').")
  2933.   (frame)
  2934.      Lisp_Object frame;
  2935. {
  2936.   register Lisp_Object tem;
  2937.   register int n_windows;
  2938.   register struct save_window_data *data;
  2939.   register int i;
  2940.   FRAME_PTR f;
  2941.  
  2942.   if (NILP (frame))
  2943.     f = selected_frame;
  2944.   else
  2945.     {
  2946.       CHECK_LIVE_FRAME (frame, 0);
  2947.       f = XFRAME (frame);
  2948.     }
  2949.  
  2950.   n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
  2951.   data = (struct save_window_data *)
  2952.            XVECTOR (Fmake_vector (make_number (SAVE_WINDOW_DATA_SIZE),
  2953.                   Qnil));
  2954.   XFASTINT (data->frame_width) = FRAME_WIDTH (f);
  2955.   XFASTINT (data->frame_height) = FRAME_HEIGHT (f);
  2956.   XFASTINT (data->frame_menu_bar_lines) = FRAME_MENU_BAR_LINES (f);
  2957. #ifdef MULTI_FRAME
  2958.   XSET (data->selected_frame, Lisp_Frame, selected_frame);
  2959. #endif
  2960.   data->current_window = FRAME_SELECTED_WINDOW (f);
  2961.   XSET (data->current_buffer, Lisp_Buffer, current_buffer);
  2962.   data->minibuf_scroll_window = Vminibuf_scroll_window;
  2963.   data->root_window = FRAME_ROOT_WINDOW (f);
  2964.   data->focus_frame = FRAME_FOCUS_FRAME (f);
  2965.   tem = Fmake_vector (make_number (n_windows), Qnil);
  2966.   data->saved_windows = tem;
  2967.   for (i = 0; i < n_windows; i++)
  2968.     XVECTOR (tem)->contents[i]
  2969.       = Fmake_vector (make_number (SAVED_WINDOW_VECTOR_SIZE), Qnil);
  2970.   save_window_save (FRAME_ROOT_WINDOW (f),
  2971.             XVECTOR (tem), 0);
  2972.   XSET (tem, Lisp_Window_Configuration, data);
  2973.   return (tem);
  2974. }
  2975.  
  2976. DEFUN ("save-window-excursion", Fsave_window_excursion, Ssave_window_excursion,
  2977.   0, UNEVALLED, 0,
  2978.   "Execute body, preserving window sizes and contents.\n\
  2979. Restores which buffer appears in which window, where display starts,\n\
  2980. as well as the current buffer.\n\
  2981. Does not restore the value of point in current buffer.")
  2982.   (args)
  2983.      Lisp_Object args;
  2984. {
  2985.   register Lisp_Object val;
  2986.   register int count = specpdl_ptr - specpdl;
  2987.  
  2988.   record_unwind_protect (Fset_window_configuration,
  2989.              Fcurrent_window_configuration (Qnil));
  2990.   val = Fprogn (args);
  2991.   return unbind_to (count, val);
  2992. }
  2993.  
  2994. init_window_once ()
  2995. {
  2996. #ifdef MULTI_FRAME
  2997.   selected_frame = make_terminal_frame ();
  2998.   minibuf_window = selected_frame->minibuffer_window;
  2999.   selected_window = selected_frame->selected_window;
  3000.   last_nonminibuf_frame = selected_frame;
  3001. #else /* not MULTI_FRAME */
  3002.   extern Lisp_Object get_minibuffer ();
  3003.  
  3004.   minibuf_window = make_window ();
  3005.   FRAME_ROOT_WINDOW (selected_frame) = make_window ();
  3006.  
  3007.   XWINDOW (FRAME_ROOT_WINDOW (selected_frame))->next = minibuf_window;
  3008.   XWINDOW (minibuf_window)->prev = FRAME_ROOT_WINDOW (selected_frame);
  3009.   XWINDOW (minibuf_window)->mini_p = Qt;
  3010.  
  3011.   /* These values 9 and 10 are arbitrary,
  3012.      just so that there is "something there."
  3013.      Correct values are put in in init_xdisp */
  3014.  
  3015.   XFASTINT (XWINDOW (FRAME_ROOT_WINDOW (selected_frame))->width) = 10;
  3016.   XFASTINT (XWINDOW (minibuf_window)->width) = 10;
  3017.  
  3018.   XFASTINT (XWINDOW (FRAME_ROOT_WINDOW (selected_frame))->height) = 9;
  3019.   XFASTINT (XWINDOW (minibuf_window)->top) = 9;
  3020.   XFASTINT (XWINDOW (minibuf_window)->height) = 1;
  3021.  
  3022.   /* Prevent error in Fset_window_buffer.  */
  3023.   XWINDOW (FRAME_ROOT_WINDOW (selected_frame))->buffer = Qt;
  3024.   XWINDOW (minibuf_window)->buffer = Qt;
  3025.  
  3026.   /* Now set them up for real.  */
  3027.   Fset_window_buffer (FRAME_ROOT_WINDOW (selected_frame),
  3028.               Fcurrent_buffer ());
  3029.   Fset_window_buffer (minibuf_window, get_minibuffer (0));
  3030.  
  3031.   selected_window = FRAME_ROOT_WINDOW (selected_frame);
  3032.   /* Make sure this window seems more recently used than
  3033.      a newly-created, never-selected window.  Increment
  3034.      window_select_count so the first selection ever will get
  3035.      something newer than this.  */
  3036.   XFASTINT (XWINDOW (selected_window)->use_time) = ++window_select_count;
  3037. #endif /* not MULTI_FRAME */
  3038. }
  3039.  
  3040. syms_of_window ()
  3041. {
  3042.   Qwindowp = intern ("windowp");
  3043.   staticpro (&Qwindowp);
  3044.  
  3045.   Qwindow_live_p = intern ("window-live-p");
  3046.   staticpro (&Qwindow_live_p);
  3047.  
  3048. #ifndef MULTI_FRAME
  3049.   /* Make sure all windows get marked */
  3050.   staticpro (&minibuf_window);
  3051. #endif
  3052.  
  3053.   DEFVAR_LISP ("temp-buffer-show-function", &Vtemp_buffer_show_function,
  3054.     "Non-nil means call as function to display a help buffer.\n\
  3055. Used by `with-output-to-temp-buffer'.");
  3056.   Vtemp_buffer_show_function = Qnil;
  3057.  
  3058.   DEFVAR_LISP ("display-buffer-function", &Vdisplay_buffer_function,
  3059.     "If non-nil, function to call to handle `display-buffer'.\n\
  3060. It will receive two args, the buffer and a flag which if non-nil means\n\
  3061.  that the currently selected window is not acceptable.\n\
  3062. Commands such as `switch-to-buffer-other-window' and `find-file-other-window'\n\
  3063. work using this function.");
  3064.   Vdisplay_buffer_function = Qnil;
  3065.  
  3066.   DEFVAR_LISP ("minibuffer-scroll-window", &Vminibuf_scroll_window,
  3067.     "Non-nil means it is the window that C-M-v in minibuffer should scroll.");
  3068.   Vminibuf_scroll_window = Qnil;
  3069.  
  3070.   DEFVAR_LISP ("other-window-scroll-buffer", &Vother_window_scroll_buffer,
  3071.     "If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window.");
  3072.   Vother_window_scroll_buffer = Qnil;
  3073.  
  3074.   DEFVAR_BOOL ("pop-up-frames", &pop_up_frames,
  3075.     "*Non-nil means `display-buffer' should make a separate frame.");
  3076.   pop_up_frames = 0;
  3077.  
  3078.   DEFVAR_LISP ("pop-up-frame-function", &Vpop_up_frame_function,
  3079.     "Function to call to handle automatic new frame creation.\n\
  3080. It is called with no arguments and should return a newly created frame.\n\
  3081. \n\
  3082. A typical value might be `(lambda () (new-frame pop-up-frame-alist))'\n\
  3083. where `pop-up-frame-alist' would hold the default frame parameters.");
  3084.   Vpop_up_frame_function = Qnil;
  3085.  
  3086.   DEFVAR_LISP ("special-display-buffer-names", &Vspecial_display_buffer_names,
  3087.     "*List of buffer names that should have their own special frames.\n\
  3088. Displaying a buffer whose name is in this list makes a special frame for it\n\
  3089. using `special-display-function'.  See also `special-display-regexps'.");
  3090.   Vspecial_display_buffer_names = Qnil;
  3091.  
  3092.   DEFVAR_LISP ("special-display-regexps", &Vspecial_display_regexps,
  3093.     "*List of regexps saying which buffers should have their own special frames.\n\
  3094. If a buffer name matches one of these regexps, it gets its own frame.\n\
  3095. Displaying a buffer whose name is in this list makes a special frame for it\n\
  3096. using `special-display-function'.  See also `special-display-buffer-names'.");
  3097.   Vspecial_display_regexps = Qnil;
  3098.  
  3099.   DEFVAR_LISP ("special-display-function", &Vspecial_display_function,
  3100.     "Function to call to make a new frame for a special buffer.\n\
  3101. It is called with one argument, the buffer,\n\
  3102. and should return a window displaying that buffer.\n\
  3103. The default value makes a separate frame for the buffer,\n\
  3104. using `special-display-alist' to specify the frame parameters.\n\
  3105. \n\
  3106. A buffer is special if its is listed in `special-display-buffer-names'\n\
  3107. or matches a regexp in `special-display-regexps'.");
  3108.   Vspecial_display_function = Qnil;
  3109.  
  3110.   DEFVAR_BOOL ("pop-up-windows", &pop_up_windows,
  3111.     "*Non-nil means display-buffer should make new windows.");
  3112.   pop_up_windows = 1;
  3113.  
  3114.   DEFVAR_INT ("next-screen-context-lines", &next_screen_context_lines,
  3115.     "*Number of lines of continuity when scrolling by screenfuls.");
  3116.   next_screen_context_lines = 2;
  3117.  
  3118.   DEFVAR_INT ("split-height-threshold", &split_height_threshold,
  3119.     "*display-buffer would prefer to split the largest window if this large.\n\
  3120. If there is only one window, it is split regardless of this value.");
  3121.   split_height_threshold = 500;
  3122.  
  3123.   DEFVAR_INT ("window-min-height", &window_min_height,
  3124.     "*Delete any window less than this tall (including its mode line).");
  3125.   window_min_height = 4;
  3126.  
  3127.   DEFVAR_INT ("window-min-width", &window_min_width,
  3128.     "*Delete any window less than this wide.");
  3129.   window_min_width = 10;
  3130.  
  3131.   defsubr (&Sselected_window);
  3132.   defsubr (&Sminibuffer_window);
  3133.   defsubr (&Swindow_minibuffer_p);
  3134.   defsubr (&Swindowp);
  3135.   defsubr (&Swindow_live_p);
  3136.   defsubr (&Spos_visible_in_window_p);
  3137.   defsubr (&Swindow_buffer);
  3138.   defsubr (&Swindow_height);
  3139.   defsubr (&Swindow_width);
  3140.   defsubr (&Swindow_hscroll);
  3141.   defsubr (&Sset_window_hscroll);
  3142.   defsubr (&Swindow_edges);
  3143.   defsubr (&Scoordinates_in_window_p);
  3144.   defsubr (&Swindow_at);
  3145.   defsubr (&Swindow_point);
  3146.   defsubr (&Swindow_start);
  3147.   defsubr (&Swindow_end);
  3148.   defsubr (&Sset_window_point);
  3149.   defsubr (&Sset_window_start);
  3150.   defsubr (&Swindow_dedicated_p);
  3151.   defsubr (&Sset_window_dedicated_p);
  3152.   defsubr (&Swindow_display_table);
  3153.   defsubr (&Sset_window_display_table);
  3154.   defsubr (&Snext_window);
  3155.   defsubr (&Sprevious_window);
  3156.   defsubr (&Sother_window);
  3157.   defsubr (&Sget_lru_window);
  3158.   defsubr (&Sget_largest_window);
  3159.   defsubr (&Sget_buffer_window);
  3160.   defsubr (&Sdelete_other_windows);
  3161.   defsubr (&Sdelete_windows_on);
  3162.   defsubr (&Sreplace_buffer_in_windows);
  3163.   defsubr (&Sdelete_window);
  3164.   defsubr (&Sset_window_buffer);
  3165.   defsubr (&Sselect_window);
  3166.   defsubr (&Sdisplay_buffer);
  3167.   defsubr (&Ssplit_window);
  3168.   defsubr (&Senlarge_window);
  3169.   defsubr (&Sshrink_window);
  3170.   defsubr (&Sscroll_up);
  3171.   defsubr (&Sscroll_down);
  3172.   defsubr (&Sscroll_left);
  3173.   defsubr (&Sscroll_right);
  3174.   defsubr (&Sscroll_other_window);
  3175.   defsubr (&Srecenter);
  3176.   defsubr (&Smove_to_window_line);
  3177.   defsubr (&Swindow_configuration_p);
  3178.   defsubr (&Sset_window_configuration);
  3179.   defsubr (&Scurrent_window_configuration);
  3180.   defsubr (&Ssave_window_excursion);
  3181. }
  3182.  
  3183. keys_of_window ()
  3184. {
  3185.   initial_define_key (control_x_map, '1', "delete-other-windows");
  3186.   initial_define_key (control_x_map, '2', "split-window");
  3187.   initial_define_key (control_x_map, '0', "delete-window");
  3188.   initial_define_key (control_x_map, 'o', "other-window");
  3189.   initial_define_key (control_x_map, '^', "enlarge-window");
  3190.   initial_define_key (control_x_map, '<', "scroll-left");
  3191.   initial_define_key (control_x_map, '>', "scroll-right");
  3192.  
  3193.   initial_define_key (global_map, Ctl ('V'), "scroll-up");
  3194.   initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
  3195.   initial_define_key (meta_map, 'v', "scroll-down");
  3196.  
  3197.   initial_define_key (global_map, Ctl('L'), "recenter");
  3198.   initial_define_key (meta_map, 'r', "move-to-window-line");
  3199. }
  3200.